Computing of angle using CalcAngleOfLineVectorDouble()

A forum dedicated to WinForms version of LightningChart Ultimate.

Moderator: Queue Moderators

Post Reply
hallo888
Posts: 43
Joined: Wed Mar 26, 2014 10:56 am

Computing of angle using CalcAngleOfLineVectorDouble()

Post by hallo888 » Tue Apr 15, 2014 6:53 am

Hi,

If i draw a line from Sydney (lat:-33.86785 long:151.20732) to Taipei (lat:25.04778 long:121.53185), the angle of the line should be around 320degrees. However, based on the following codes, i got 253 degrees.

Code: Select all

PointFloat p1 = new PointFloat(m_chart.ViewXY.XAxes[0].ValueCoord(151.20732f),m_chart.ViewXY.YAxes[0].ValueCoord(-33.86785f)); //from sydney
PointFloat p2 = new PointFloat(m_chart.ViewXY.XAxes[0].ValueCoord(121.53185f),m_chart.ViewXY.YAxes[0].ValueCoord(25.04778f)); //to taipei

double angle = ChartTools.CalcAngleOfLineVectorDouble(p1,p2)*(180/Math.PI);
May i know what is wrong with my codes?

Thanks!

User avatar
ArctionPasi
Posts: 1367
Joined: Tue Mar 26, 2013 10:57 pm
Location: Finland
Contact:

Re: Computing of angle using CalcAngleOfLineVectorDouble()

Post by ArctionPasi » Tue Apr 15, 2014 7:16 am

It should work only for axes same amount of pixels in X and Y represent the same length (lat or long degree amount). So that's not probably not suitable for maps where they seldom are equal.

This should work if you set ViewXY.ZoomPanOptions.AspectRatioOptions.AspectRatio = Manual, and .ManualAspectRatio = 1.
LightningChart Support Team, PT

hallo888
Posts: 43
Joined: Wed Mar 26, 2014 10:56 am

Re: Computing of angle using CalcAngleOfLineVectorDouble()

Post by hallo888 » Tue Apr 15, 2014 7:38 am

ArctionPasi wrote:It should work only for axes same amount of pixels in X and Y represent the same length (lat or long degree amount). So that's not probably not suitable for maps where they seldom are equal.

This should work if you set ViewXY.ZoomPanOptions.AspectRatioOptions.AspectRatio = Manual, and .ManualAspectRatio = 1.
Hi Pasi,

Thanks for your reply. You mentioned that the function only works when X and Y axes have the same amount of pixels which is not the case in the map, so does that mean that this function will not work in map? For your information, I have set the following but still end up with the same angle. By the way, i only found .ManualAspectRatioWH and not ManualAspectRatio.

Code: Select all

m_chart.ViewXY.ZoomPanOptions.AspectRatioOptions.AspectRatio = ViewAspectRatio.Manual;
m_chart.ViewXY.ZoomPanOptions.AspectRatioOptions.ManualAspectRatioWH = 1;

User avatar
ArctionPasi
Posts: 1367
Joined: Tue Mar 26, 2013 10:57 pm
Location: Finland
Contact:

Re: Computing of angle using CalcAngleOfLineVectorDouble()

Post by ArctionPasi » Tue Apr 15, 2014 9:33 pm

I'm just trying to say, that using aspect ratio of 1:1, the angle is different than you probably have anticipated. When I plot a route from Sydney to Taipei with 1:1 ratio, it looks like the following:
Sydney-Taipei with a marker with bitmap symbol and angle of 116.7 degrees
Sydney-Taipei with a marker with bitmap symbol and angle of 116.7 degrees
sydney-taipei_aspect_1.jpg (342.78 KiB) Viewed 8331 times
I've added a yellow arrow marker with rotate angle of 116.7, the same value than the ChartTools.CalcAngleOfLineVectorDouble returns (converted to degrees). The angle is correct. Angle 0 is straight to the right in LightningChart objects.

But the map very rarely is with 1:1 aspect ratio, and therefore the ChartTools.CalcAngleOfLineVectorDouble can't be used directly. I'll try to figure out the correct way to compute the angle.
LightningChart Support Team, PT

User avatar
ArctionPasi
Posts: 1367
Joined: Tue Mar 26, 2013 10:57 pm
Location: Finland
Contact:

Re: Computing of angle using CalcAngleOfLineVectorDouble()

Post by ArctionPasi » Tue Apr 15, 2014 10:02 pm

Ok, I thing I came up with correct formula.

Code: Select all

public double CalcAngleOnScreen(PointFloat pointFrom, PointFloat pointTo, AxisX xAxis, AxisY yAxis)
        {
            const double cdPI_Per2 = System.Math.PI / 2.0;
            
            double dAngle;
            double dXDif = pointTo.X - pointFrom.X;
            double dYDif = pointTo.Y - pointFrom.Y;

            double dXRange = xAxis.Maximum - xAxis.Minimum;
            double dYRange = yAxis.Maximum - yAxis.Minimum;

            double dGraphWidthInPixels = m_chart.ViewXY.GetMarginsRect().Width;
            double dGraphHeightPixels = m_chart.ViewXY.GetMarginsRect().Height;

            double dAspect = (dXRange / dGraphWidthInPixels) /  (dYRange / dGraphHeightPixels); 

            if (dXDif == 0)
            {
                if (pointTo.Y > pointFrom.Y)
                    dAngle = cdPI_Per2;
                else
                    dAngle = -cdPI_Per2;
            }
            else
            {
                if (dYDif >= 0)
                {
                    //First quarter (0...Pi/2)
                    if (dXDif > 0) //== 0 is prevented earlier 
                        dAngle = Math.Atan(dYDif / dXDif * dAspect);//atan positive

                    //Second quarter
                    else
                        dAngle = Math.PI + Math.Atan(dYDif / dXDif * dAspect); //atan is negative
                }
                else
                {
                    //Third quarter 
                    if (dXDif < 0)
                        dAngle = Math.PI + Math.Atan(dYDif / dXDif * dAspect); //atan positive

                    //Fourth
                    else
                        dAngle = 2.0 * Math.PI + Math.Atan(dYDif / dXDif * dAspect); //atan is negative
                }
            }
            return dAngle;
        }
Supply the points and X axis and Y axis.

Then it gives correct angle for the marker. Here aspect ratio is 0.5.
Sydney-Taipei with aspect ratio 1:2
Sydney-Taipei with aspect ratio 1:2
sydney-taipei_aspect_1_2.jpg (273.9 KiB) Viewed 8331 times
LightningChart Support Team, PT

Post Reply