-1

I'm creating a C# code which draws a path. The problem is I don't have coordinates of the path vertices. Instead, I know length of each segment and angle between adjacent segments.

Assuming the first point of the path has coordinates (0;0) I want to draw the path calculating every vertex from given segment length and angle. I'm not good in trigonometry, but I hope it is possible.

I try to cycle through the collection of segments to calculate next point coordinate at each step. So at any step I have the following data:

enter image description here

Given first segment AB with length L1, next segment BC with length L2, an angle ABC between segments AB and BC. Coordinates of points A and B are know, because are evaluated on the previous step.

If it is possible, how to calculate coordinates of the point C from the given data?

This is an example of a collection of segments:

    public ObservableCollection<SequenceStep> Sequence { get; set; }

where:

    public class SequenceStep
    {
        public double Length { get; set; }
        public double Angle { get; set; }
    }

I cycle through the sequence like this:

    for (var i = 1; i < Sequence.Count; i++)
    {
        var sequenceStep = Sequence[i];
        var angleInRadians = Math.PI * sequenceStep.Angle / 180.0;

        // Calculate next point coordinates from (0,0)

        var x = Math.Cos(angleInRadians) * sequenceStep.Length;
        var y = Math.Sin(angleInRadians) * sequenceStep.Length;
    }

// I start from segment[1], because segment[0] has points (0,0) and (segment[0].Length, 0).

But evaluated coordinates are only for the angle between point and axis X. I think I need to rotate those x,y coordinates, to correspond with orientation of the segment BC. But I always get wrong coordinates.

I would appreciate any help, a C# method or a set formulas.

pixelhound
  • 13
  • 3
  • 3
    This is a math question, and nothing to do with programming – TheGeneral Jul 19 '19 at 01:52
  • Thanks for your help. The question is how to program it with C#. – pixelhound Jul 19 '19 at 02:00
  • As I mentioned, I'm not good in trigonometry, so if I ask this question in math.stackexchange.com, I would probably get an answer I can't understand. So I'm looking for help from other C# developers, who worked with such task and can share his portion of code or give formula in C# language. – pixelhound Jul 19 '19 at 02:05
  • 2
    I'm voting to close this question as off-topic because this is strictly a question about math, not about programming. Try looking for an answer on https://math.stackexchange.com/ (emphasis on _"looking"_...you definitely should search there first, before posting an actual question) – Peter Duniho Jul 19 '19 at 02:21
  • 1. Work out the angle of AB relative to 0. 2. Combine that with the angle AB, BC. Do a calculation similar to what you already have based on this angle. – ProgrammingLlama Jul 19 '19 at 02:21
  • This is very similar question to: https://stackoverflow.com/questions/7586063/how-to-calculate-the-angle-between-a-line-and-the-horizontal-axis?rq=1 and it has 386 upvotes and acepted answer. What is wrong with my question? I ask for C# help in developing a small portion of code. The explanation too long, I agree but the question is very similar to hundreds of other questions here. – pixelhound Jul 19 '19 at 02:25
  • I understand, I have a little reputation here, but I'm not new here nor in development. I know how to look and ask for answers. If you don't have an answer, just let other people help me. – pixelhound Jul 19 '19 at 02:28
  • @pixelhound John already has answered - just accumulate angle as sum of previous absolute angle and current relative angle at every step – MBo Jul 19 '19 at 02:49
  • 1
    Its all our duty to keep the site specific, on topic, and valued for future users. It just so happens this is better suited to the math site. also just because there are previous answers of similar questions 8 years ago, does not mean that this is still relevant for a programming site today. If you would like to discuss this further or ask for qualification you are most welcome to start a question on the StackOverflow meta discussion site. – TheGeneral Jul 19 '19 at 02:53
  • @John, MBo - thank you for you help! I understood the idea, I'll try this out. – pixelhound Jul 19 '19 at 02:56
  • _"This is very similar question to: stackoverflow.com/questions/7586063/… and it has 386 upvotes and acepted answer. What is wrong with my question?"_ -- that in the past, users have not only failed to close an off-topic question, but eventually up-voted that question highly, does not make that question a good one. Stack Overflow is based on adherence to standards, not precedence, and two wrongs don't make a right. – Peter Duniho Jul 19 '19 at 03:31

1 Answers1

1

You can accumulate the angle and calculate the positions like you already do:

double angle = 0.0;
double x = Seqence[0].Length;
double y = 0.0;
for (var i = 1; i < Sequence.Count; i++)
{
    var sequenceStep = Sequence[i];
    var angleInRadians = Math.PI * sequenceStep.Angle / 180.0;

    // update the current angle
    angle += Math.PI - angleInRadians; 
    //    ^-- might also be -= depending on your definition of orientation

    // Calculate next point coordinates

    x += Math.Cos(angle) * sequenceStep.Length;
    y += Math.Sin(angle) * sequenceStep.Length;

    // Now use (x, y)
}
Nico Schertler
  • 32,049
  • 4
  • 39
  • 70