-1

I'm trying to find the cursor position related to the red line in the image below.

I tried these topics: Using atan2 to find angle between two vectors And How to calculate the angle between a line and the horizontal axis? but using Math.Atan2

But there is a problem, P1 and P2 have not same values if I use these methods.

Is there any method to get any position of any points on a UIElement (e.g. Ellipse) using the red vector such as every point with the same angle (here P1 and P2) has the same value ?

enter image description here

Selim
  • 37
  • 5

1 Answers1

2

Yes, and atan2 is perfectly the needed method. We can use cross- and dot-product to achieve result:

 bx = redline_end.x - center.x
 by = redline_end.y - center.y
 // here bx=0 and by=75
 px = p1.x - center.x
 py = p1.y - center.y
 angle = atan2(px * by - py * bx, px * bx + py * by)  //and similar for P2
MBo
  • 77,366
  • 5
  • 53
  • 86
  • Just tried it out and it looks like it works perfectly ! Just to be sure that I understood it: `redline_end.x` is 0 and `redline_end.Y` is `Height/2` ? – Selim Dec 05 '18 at 09:36
  • Not exactly - here `0` and`Height/2` corresponds to coordinate **differences** between end and start of the red line (bx, by), not to coordinates themselves. – MBo Dec 05 '18 at 09:43