Just Ask
Just Ask

Reputation: 339

Draw 3D arc given start, middle, and endpoint

I am trying to make a user-defined arc with the Helix 3D toolkit. The user selects 3 points on the arc (start, middle, end) and the program finds the center of the circle and draws the arc from start to end. My problem is I'm not good at math and I am having problems making this work. My main problem is getting the start and end angles and having it draw arcs of all sizes accurately. Any help is appreciated. Here is my code:

private void Draw_Arc(object sender, MouseButtonEventArgs e)
    {
            linept = new List<Point3D>();
            linept.Add(startPoint);
            linept.Add(endPoint);
            linept.Add((Point3D)GetPoints(e));
            LinesVisual3D line = new LinesVisual3D();
            line.Thickness = 2;
            line.Color = Colors.Blue;
            line.Points = linept;
            port.Children.Add(line);

            double startAngle, sweepAngle;
            Point3D center = GetCenterOfArc(linept.ElementAt(0), linept.ElementAt(1), linept.ElementAt(2));
            GetAngles(linept.ElementAt(0), linept.ElementAt(1), linept.ElementAt(2), out startAngle, out sweepAngle);
            circle = new PieSliceVisual3D();
            double RadiusX = Math.Abs(startPoint.X - center.X);
            double RadiusY = Math.Abs(startPoint.Y - center.Y);
            circle.Center = center;
            if (RadiusX >= RadiusY)
                circle.OuterRadius = RadiusX;
            else
                circle.OuterRadius = RadiusY;
            circle.InnerRadius = circle.OuterRadius + 3;
            circle.StartAngle = (180 / Math.PI * Math.Atan2(startPoint.Y - circle.Center.Y, startPoint.X - circle.Center.X));
            circle.EndAngle = (180 / Math.PI * Math.Atan2(linept.ElementAt(2).Y - circle.Center.Y, linept.ElementAt(2).X - circle.Center.X));
            port.Children.Add(circle);
    }

Upvotes: 0

Views: 2448

Answers (1)

YoryeNathan
YoryeNathan

Reputation: 14512

I think that you have to know the center of the circle in order to know the starting and ending angle of the arc.

Say that you just have three points, and you want to find a circle that goes through all three, you basically have three equations with three variables:

(x-x0)^2 + (y-y0)^2 = R^2

(x-x1)^2 + (y-y1)^2 = R^2

(x-x2)^2 + (y-y2)^2 = R^2

Solving that can get a little tricky if you try to program that on your own and have average knowledge in math, but you can do it fairly easily using matrices. Read here for a bit information.

After you've solved the three equations, you should have X, Y, R.

X and Y will be the center point of the circle, and R - it's radius.

Now, as far as I remember, they count the arc's degrees starting from the positive X axis, going upwards. So you would need to calculate the angle between two lines - the line that stretches between the center to your floating point, and the line that stretches from your center point to the "limitless" right. You may just Google "calculate angle between two lines". Repeating that process for both your starting point and your ending point, will give each their respective entering/exiting angle.

The middle point isn't really used anymore, but the radius is. You just set it to be the radius and you're good to go.

I haven't really implemented anything - just giving you a fair direction. (and I bet that there's a much cleaner and nicer-to-work-with solution)

Upvotes: 2

Related Questions