How to change the color when drawing a line segment that belongs to a circle?

Asked

Viewed 126 times

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:

circul e reta

  • 1

    congratulations on the question! Upvote on the right!

1 answer

7


You already have the answer to your question. If you know how to verify if a point belongs to the circle, just do the following check for each point of the straight you will paint:

And your code already does that. See that function eqCircle returns -1 if the point is outside the circle, 0 whether it is internal to the circle and 1 if it belongs to the circumference.

Therefore, just change the color when printing one of the points of the line if the value of eqCirculo have returned 0 (that is, the point is internal to the circumference). We only need to touch the part of the for.

Thus:

for x in range(plano.shape[0]):
    for y in range(plano.shape[1]):
        resultadoCirculo = eqCirculo(x, y)

        # Se pertence à reta:
        if eqReta(x, y) == 1:
            if resultadoCirculo == 0:
                # Verde se o ponto é interno à circunferência.
                plano[x, y] = [0, 255, 0]
            else:
                # Vermelho se o ponto é externo à circunferência.
                plano[x, y] = [255, 0, 0]

        if resultadoCirculo == 1:
            plano[x, y] = [0, 0, 255]  # Azul se pertence à circunferência.

The result will be something like this:

Resultado esperado

  • 3

    I really I loved answering that question. First because I had never messed with this Python library. Second and most significant because last year I studied Introduction to Analytical Geometry in school and I thought it would take a while to find a practical application. Why, I was wrong: here it is! D Massa demais...

  • 2

    Programming is a really efficient way to train mathematically.

  • 2

    @Luiz Felipe, elegant and concise answer. + 1.

  • 1

    @Luizfelipe congratulations! What a sensational response! I wish the universities had, like you, professors in the math course for some practical discipline with certain incredible exercises like this. My congratulations man, I wish you much success!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.