python怎麼畫長方形 畫乙個漂亮的長方形

2021-10-19 03:11:30 字數 1820 閱讀 2160

您可以通過使用繪製lines和arcs的函式來實現您想要的。在

要繪製的框架由4個相似的部分組成(每個角乙個),每個部分旋轉(或映象)。在

讓我們看看左上角:

如您所見,我們需要繪製2條線段(長度為d)和一條弧(半徑為r的圓的四分之一)。在

假設左上角的座標是(x1, y1)。在

這意味著弧的中心位於(x1 + r, y1 + r)。在

其中一行將從(x1 + r, y1)到(x1 + r + d, y1)。在

另一行將從(x1, y1 + r)到(x1, y1 + r + d)。在

其他角落也會發生類似的情況。在

示例**:import cv2

import numpy as np

def draw_border(img, pt1, pt2, color, thickness, r, d):

x1,y1 = pt1

x2,y2 = pt2

# top left

cv2.line(img, (x1 + r, y1), (x1 + r + d, y1), color, thickness)

cv2.line(img, (x1, y1 + r), (x1, y1 + r + d), color, thickness)

cv2.ellipse(img, (x1 + r, y1 + r), (r, r), 180, 0, 90, color, thickness)

# top right

cv2.line(img, (x2 - r, y1), (x2 - r - d, y1), color, thickness)

cv2.line(img, (x2, y1 + r), (x2, y1 + r + d), color, thickness)

cv2.ellipse(img, (x2 - r, y1 + r), (r, r), 270, 0, 90, color, thickness)

# bottom left

cv2.line(img, (x1 + r, y2), (x1 + r + d, y2), color, thickness)

cv2.line(img, (x1, y2 - r), (x1, y2 - r - d), color, thickness)

cv2.ellipse(img, (x1 + r, y2 - r), (r, r), 90, 0, 90, color, thickness)

# bottom right

cv2.line(img, (x2 - r, y2), (x2 - r - d, y2), color, thickness)

cv2.line(img, (x2, y2 - r), (x2, y2 - r - d), color, thickness)

cv2.ellipse(img, (x2 - r, y2 - r), (r, r), 0, 0, 90, color, thickness)

img = np.zeros((256,256,3), dtype=np.uint8)

draw_border(img, (10,10), (100, 100), (127,255,255), 1, 10, 20)

draw_border(img, (128,128), (240, 160), (255,255,127), 1, 5, 5)

cv2.imwrite('round_rect.png', img)

結果:

OpenGL學習筆記 2 畫乙個正方形

void baseinit glfwmakecontextcurrent glwindow if gladloadglloader gladloadproc glfwgetprocaddress 畫乙個普通的正方形 void normalsquare 索引 unsigned int indices ...

OpenGL學習筆記 2 畫乙個正方形

void baseinit glfwmakecontextcurrent glwindow if gladloadglloader gladloadproc glfwgetprocaddress 畫乙個普通的正方形 void normalsquare 索引 unsigned int indices ...

用Python的turtle畫乙個正方形圓形五角星

我在學習python時做的乙個簡單的正方形中夾乙個圓形再夾乙個五角星 如下圖所示 畫正方形 t.begin fill for i in range 4 t.forward 200 t.right 90 t.end fill t.right 90 調整畫筆位置 t.forward 100 t.fill...