7
I’m trying to draw in an image 400 × 400
. The first drawing is a line defined by the equation ax + by + c = 0
. When to replace x
and y
and the result is 0 the point belongs to straight, then change the color to blue.
The other drawing is a circle defined by (x - xc)² + (y – yc)² = R²
, where the root of the first part is equal to radius (R). To find out if a point belongs to the circle, compare the distance (d) from the point P(x, y) to the center and, if that value is equal to the radius, the point belongs to the circumference and, if it is smaller, is internal to the circle.
So far so good, but I need the part of the line inside the circle to be a different color from the rest of the line. Someone knows how to do it?
import cv2
import numpy as np
def eqReta(x, y, pA=[0, 0], pB=[5, 5]):
a = pA[1] - pB[1] # a = (ya - yb)
b = pB[0] - pA[0] # b = (xb - xa)
c = (pA[0] * pB[1]) - (pB[0] * pA[1]) # c = xayb - xbya
f = int(a * x + b * y + c) # equacao geral
return f == 0 # ponto x, y pertence a reta
def eqCirculo(x, y, center=[150, 200], r=75):
d = int(np.sqrt((x - center[0]) ** 2 + (y - center[1]) ** 2))
if d > r: # ponto x, y fora do circulo
return -1
if d < r: # ponto x, y interno ao circulo
return 0
if d == r: # ponto x, y esta na borda do circulo
return 1
plano = np.ones((400, 400, 3)) * 255
for x in range(plano.shape[0]):
for y in range(plano.shape[1]):
if eqCirculo(x, y) == 1:
plano[x, y] = [0, 0, 255]
if eqReta(x, y) == 1:
plano[x, y] = [255, 0, 0]
cv2.imshow("Canvas", plano)
cv2.waitKey(0)
The code I already have generates this image:
congratulations on the question! Upvote on the right!
– Mateus