Round #172 (Div. 2) C. Rectangle Puzzle

题意:给出一个矩形,绕中心旋转一个角度,得到一个新的矩形,求两个矩形重叠的部分的面积。

用计算几何求线段交点的方法肯定可以做,嫌代码太长,实在懒得写。其实这题可以用纯几何的方式求出公式解出来。首先利用对称性,只考虑旋转角度小于90度,且长大于宽的情况。还有一个简单的情况就是当角度大于2 *  atan(h / w)的时候,重叠面积就是一个平行四边形了。对于一般情况,做两条辅助线就很容易求出上方三角形的高了。




from math import atan, sin, tan, cos, pi

w, h, a = map(float, raw_input().split())
if w < h: w, h = h, w
if a > 90.: a = 180. - a
a = a * pi / 180.
if a >= 2. * atan(h / w):
    print h * (h / sin(a))
else:
    x1 = ((w / 2.) * tan(a) + (h / 2.)) * cos(a) - (h / 2.)
    x2 = ((h / 2.) * tan(a) + (w / 2.)) * cos(a) - (w / 2.)
    area1 = x1 * (x1 / tan(a) + x1 * tan(a)) if x1 != 0. else 0.
    area2 = x2 * (x2 / tan(a) + x2 * tan(a)) if x2 != 0. else 0.
    print w * h - area1 - area2


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这是关于使用OpenCV进行人脸检测的基本步骤: 1. 导入OpenCV库 ```python import cv2 ``` 2. 读取图片 ```python img = cv2.imread('image.jpg') ``` 3. 加载人脸模型(级联分类器)特征人脸建模 ```python face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml') ``` 4. 对图片进行灰度处理 ```python gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) ``` 5. 检查人脸 ```python faces = face_cascade.detectMultiScale(gray, scaleFactor=1.3, minNeighbors=5) ``` 6. 标记人脸 ```python for (x, y, w, h) in faces: cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2) ``` 7. 显示图片 ```python cv2.imshow('img', img) ``` 8. 暂停窗口 ```python cv2.waitKey(0) ``` 9. 销毁窗口 ```python cv2.destroyAllWindows() ``` 完整代码如下: ```python import cv2 # 读取图片 img = cv2.imread('image.jpg') # 加载人脸模型(级联分类器)特征人脸建模 face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml') # 对图片进行灰度处理 gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # 检查人脸 faces = face_cascade.detectMultiScale(gray, scaleFactor=1.3, minNeighbors=5) # 标记人脸 for (x, y, w, h) in faces: cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2) # 显示图片 cv2.imshow('img', img) # 暂停窗口 cv2.waitKey(0) # 销毁窗口 cv2.destroyAllWindows() ``` 注意:在运行这段代码之前,需要先下载并保存级联分类器文件(haarcascade_frontalface_default.xml)在与代码文件相同的目录下。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值