0
$\begingroup$

I am (with wonderful help from this site) developing a number of VBA routines to drive some shape-related activity in Powerpoint. For example, I have a circle with a line segment that starts in the center of the circle, and radiates outward in a random direction.

Right now, I'm trying to determine the nearest anchor point on the circle to where the line intersects (N, NE, E, SE, S, SW, W, NW). I'm well on my way when dealing with most lines, but if the line segment is perfectly vertical ($x_2-x_1 = 0$) or horizontal ($y_2-y_1=0$) then my slope equation $\left[m = \frac{(y_2 - y_1)}{ (x_2 - x_1)}\right]$ returns either a zero numerator or denominator, and then I get stuck trying to figure out which compass point is the closest one.

Is there perhaps a better approach than using the slope of the line segment? If I have the x,y coordinate of the intersect, perhaps there is an equation that can convert that to a degree (out of $360^\circ$) and then I could find the nearest compass point with simple division?

Thanks for any advice/help, especially anything that I can easily convert into VBA code ;-)

$\endgroup$
0
$\begingroup$

$\arctan(y/x)$ would be an angle in radians, and you have to be careful of the signs of $y$ and $x$. Check for a function called atan2, and make sure you convert radians to degrees. The function atan2 takes care of all the special cases of zeros in the denominator and figures out the quadrants, i.e. that $(x,-y)$ is at a different angle than $(-x,y)$ even though the ratio is $-y/x$ in either case. Same goes for the pair $(x,y)$ and $(-x,-y)$.

As for snapping to a compass angle, it seems that modulo division should help you out.

$\endgroup$
  • $\begingroup$ Marking as answered; powerpoint VBA doesn't have tan, cos, etc so I would have had to do all that from scratch. Instead, I just used the slope to determine the nearest point using a select case statement and 'manual' upper and lower limits on the angle. Thanks for the ideas though! Maybe I can use that in the future in Excel VBA. $\endgroup$ – Keith Jun 19 '13 at 21:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.