Hamolicious
Hamolicious

Reputation: 43

How do I determine a direction vector for a point on a circle to be away from a circle?

I need to get a directional vector [x, y] to point exactly away from a circle that the point is on.

I tried to just assign the x and y coordinates to the point but that points them all in the same direction

r = 70 # radius of circle
a = 0 # angle var

#create a while loop as long as the angle is bellow 2pi
while a < pi*2:
    # create an x and a y coordinate around the circle
    x = int(r * cos(a)) + 200
    y = int(r * sin(a)) + 200

    # add a ray class on that point # \/ I want to figure out these vectors
    rays += [(rayClass.Ray(x, y, 1, [direction x, direction y]))]

    # increment the angle slightly
    a += 0.01

The ray class is just a point that will cast a line in the given xy coordinates

I need the direction x and y to point exactly away from the center of the circle that they are on. How would I go across calculating them?

Upvotes: 0

Views: 3071

Answers (2)

Rabbid76
Rabbid76

Reputation: 210890

The direction vector from the center of the circle to the point is:

dx, dy = cos(a), sin(a)

This is the same direction as from the normal vector to the point on the circle. So the ray can be set up by:

x = round(r * dx) + 200
y = round(r * dy) + 200

rays += [(rayClass.Ray(x, y, 1, [dx, dy]))] 

Note, in this case the direction vector is a Unit vector (the length of a unit vector is 1) and so the components are floating point numbers.

If the direction has to be integral and its length has to be the radius of the circle:

rx, ry = round(r * cos(a)), round(r * sin(a))
x, y   = rx + 200, ry + 200

rays += [(rayClass.Ray(x, y, 1, [rx, ry]))]

Another possibility is to use pygame.math.Vector2 for vector arithmetic in PyGame.

A direction vector can be calculated by a --operation and a Unit vector (the length of a unit vector is 1) can be get by .normalize():

circle_center   = pygame.math.Vector2(200, 200)
point_on_circle = pygame.math.Vector2(x, y)
direction       = point_on_circle  - circle_center
unit_direction  = direction.normalize()
rays += [(rayClass.Ray(x, y, 1, [unit_direction[0], unit_direction[1]]))]

Upvotes: 1

Prune
Prune

Reputation: 77837

You get your direction from the radius that ends at that point. From your sparse code presentation, I gather that your circle is centered at the origin, and that your "ray" is defined primarily by the end point and any other one point on the ray.

That makes it simple: the slope (direction) of the ray is

m = (y-0)/(x-0)

with a starting point of (x, y). Thus, the equation of the line containing the ray is

y' = mx'   for variables x', y', x' >= x

Therefore, your ray is trivial: endpoint of (x, y) (which you already knew) and a directional vector of [x, y]. To be safe, I'll suggest doubling that, to make sure that the latter refers to a point on the ray (outside the circle).

(x, y, 1, [2*x, 2*y])

I can't be sure, since you utterly failed to define your class, but this gives you a second point exactly one radius outside the circle.

Upvotes: 0

Related Questions