1

I have a circle and want to draw a line inside that circle from the center (50|50) to the edge of the circle through the point i clicked on.

To test things, I already added this to my Circle.MouseDown event which draws the line from the center to the point I clicked on:

var PosX2 = e.GetPosition(MyCircle).X;
var PosY2 = e.GetPosition(MyCircle).Y;

CircleLine.X1 = 50;
CircleLine.Y1 = 50;
CircleLine.X2 = PosX2;
CircleLine.Y2 = PosY2;

What do I need to add to "stretch" my line to the edge of the Circle?

Nigel-Lee
  • 145
  • 1
  • 13
  • Deleted since I missed "ellipse" vs "circle". – ProgrammingLlama May 22 '17 at 08:26
  • @john It's a little more complicated for a general ellipse (rather than the special case of a circle). – Matthew Watson May 22 '17 at 08:28
  • @MatthewWatson Sorry, for some reason I had a brain fart and it said to me "circle", not "ellipse." – ProgrammingLlama May 22 '17 at 08:30
  • @John You are both right, im the one brainfartig here. Since the element is called "Ellipse" in wpf, i somehow thought i created an Ellipse while it is only a simple Circle. Changing the words in post. – Nigel-Lee May 22 '17 at 08:37
  • 1
    @john In the light of the recent information, you should re-instate your post! – Matthew Watson May 22 '17 at 08:39
  • 2
    Oh, OK: To repeat myself, you want to find the position of the point you clicked _relative_ to the centre of the circle. Use that information to calculate the angle that the line is at. Then use the code [here](http://stackoverflow.com/questions/674225/calculating-point-on-a-circles-circumference-from-angle-in-c) to calculate the position on the circle itself. – ProgrammingLlama May 22 '17 at 08:45

2 Answers2

2

Think of it as a problem of lengthening a vector.

Suppose that you are given a circle with center (a,b) and radius r and that the point (c,d) lies in the circle at a point other than the center.

Then, the vector from (a,b) to (c,d) is (c-a, d-b). Its length is

L = sqrt((c-a)^2 + (d-b)^2)

But then the vector r/L * (c-a,d-b) has length r and points in the same direction as the ray from (a,b) to (c,d). Simply translate this vector by the center, and you get the desired point on the circle:

(a + r/L * (c-a), b + r/L * (d-b))

Just connect the center with the above point.

John Coleman
  • 51,337
  • 7
  • 54
  • 119
1

Johns comment got me the right answer, thank you!

I just wanted to post the solution:

double X1 = 75;
double Y1 = 75;
double X2 = e.GetPosition(CircleButtonCanvas).X;
double Y2 = e.GetPosition(CircleButtonCanvas).Y;
double startPoint = 17;
double endPoint = 48;

double xDiff = X2 - X1;
double yDiff = Y2 - Y1;
double Angle = Math.Atan2(yDiff, xDiff) * (180 / Math.PI);
CircleLine.X1 = 75 + (endPoint* Math.Cos(Angle * (Math.PI / 180)));
CircleLine.X2 = 75 + (startPoint * Math.Cos(Angle* (Math.PI / 180)));
CircleLine.Y1 = 75 + (endPoint * Math.Sin(Angle* (Math.PI / 180)));
CircleLine.Y2 = 75 + (startPoint * Math.Sin(Angle* (Math.PI / 180)));

With 75|75 beeing the center of my circle

Nigel-Lee
  • 145
  • 1
  • 13