1

I have a c# program where I need to draw some simple 2D objects on the canvas.

One of these involves drawing a rectangle and lines where I know the start point, the length and I have to calculate the end position. So I have the following code;

 private void CalculateEndPoint()
 {
            double angle = Helper.deg2rad((double)this.StartAngle);
            int x = this.StartPoint.X + (int)(Math.Cos(angle) * this.Length * -1);

            int y = this.StartPoint.Y + (int)(Math.Sin(angle) * this.Length);
            this.EndPoint = new Point(x, y);
 }

Now this seems to work OK to calculate the end points. The issue I have is with the angle (this.StartAngle), the value I specify seems not to be how it is drawn and I seem to have the following;

Circle degrees

Where as I'm expecting 0 at the top, 90 on the right, 180 at the bottom etc.

So to get a shape to draw straight down the canvas I have to specify 90 degrees, where as I would expect to specify 180.

Have I done something wrong? Or is it just a lack of understanding?

jason.kaisersmith
  • 8,712
  • 3
  • 29
  • 51

2 Answers2

1

Actually, 0 should be on the right. You are multiplying the x-coordinate by -1, so you're moving it to the left.
Just remember these 2 rules:
- The cosine of the angle is the x-coordinate of the unit circle.
- The sine of the angle is the y-coordinate of the unit circle.
Since cos(0) = 1 and sin(0) = 0, the coordinate corresponding to angle 0 is (1, 0).

Whether 90 is on top or on the bottom depends on the canvas.
Some applications/frameworks consider y-coordinate 0 to be at the top of the canvas. That means you go clockwise around the circle and 90 will be at the bottom.
If y-coordinate 0 is at the bottom of the canvas, you go counter-clockwise and 90 will be at the top.

Dennis_E
  • 8,751
  • 23
  • 29
1

You should change your CalculateEndPoint function to have that:

private static void CalculateEndPoint(double dec)
{
    double angle = (Math.PI / 180) * (this.StartAngle + 90); // add PI / 2
    int x = StartPoint.X + (int)(Math.Cos(angle) * Length * -1);

    double angle2 = (Math.PI / 180) * (this.StartAngle - 90); // minus PI / 2
    int y = StartPoint.Y + (int)(Math.Sin(angle2) * Length);
    EndPoint = new Point(x, y);
}
NoName
  • 7,940
  • 13
  • 56
  • 108