画方入圆
Draw inscribe square into the circle
我想在圆中画内切方,但我不知道,我应该如何计算起点和终点:
import numpy as np
import cv2
img = np.zeros([300,300,3],dtype=np.uint8)
img.fill(255) # or img[:] = 255
imageWithCircle = cv2.circle(img, (150,150), 60, (0, 0, 255), 2)
#startpoint = ...
#endpoint = ...
imageWithInscribingSquare = cv2.rectangle(imageWithCircle, startpoint, endpoint, (0, 0, 255) , 2)
cv2.imshow("Circle", imageWithCircle)
cv2.waitKey(0)
cv2.destroyAllWindows()
检查这个:
import numpy as np
import cv2
img = np.zeros([300,300,3],dtype=np.uint8)
img.fill(255) # or img[:] = 255
imageWithCircle = cv2.circle(img, (150,150), 60, (0, 0, 255), 2)
r = 60
startpoint = (int(150+(r/(2**0.5))),int(150-(r/(2**0.5))))
endpoint = (int(150-(r/(2**0.5))),int(150+(r/(2**0.5))))
print(startpoint,print(endpoint))
imageWithInscribingSquare = cv2.rectangle(imageWithCircle, startpoint, endpoint, (255, 0, 0) , 2)
cv2.imshow("Circle", imageWithCircle)
cv2.waitKey(0)
cv2.destroyAllWindows()
输出:
计算:
如果半径为'r',则正方形的边长为√2r,从中心开始点的宽度减少√2r/2,高度增加√2r/2,端点反之亦然。
我想在圆中画内切方,但我不知道,我应该如何计算起点和终点:
import numpy as np
import cv2
img = np.zeros([300,300,3],dtype=np.uint8)
img.fill(255) # or img[:] = 255
imageWithCircle = cv2.circle(img, (150,150), 60, (0, 0, 255), 2)
#startpoint = ...
#endpoint = ...
imageWithInscribingSquare = cv2.rectangle(imageWithCircle, startpoint, endpoint, (0, 0, 255) , 2)
cv2.imshow("Circle", imageWithCircle)
cv2.waitKey(0)
cv2.destroyAllWindows()
检查这个:
import numpy as np
import cv2
img = np.zeros([300,300,3],dtype=np.uint8)
img.fill(255) # or img[:] = 255
imageWithCircle = cv2.circle(img, (150,150), 60, (0, 0, 255), 2)
r = 60
startpoint = (int(150+(r/(2**0.5))),int(150-(r/(2**0.5))))
endpoint = (int(150-(r/(2**0.5))),int(150+(r/(2**0.5))))
print(startpoint,print(endpoint))
imageWithInscribingSquare = cv2.rectangle(imageWithCircle, startpoint, endpoint, (255, 0, 0) , 2)
cv2.imshow("Circle", imageWithCircle)
cv2.waitKey(0)
cv2.destroyAllWindows()
输出:
计算:
如果半径为'r',则正方形的边长为√2r,从中心开始点的宽度减少√2r/2,高度增加√2r/2,端点反之亦然。