none
Shape Math RRS feed

  • Question

  • Does anyone have a link to math functions to find points in shapes such as....

    isPointInPolygon

    getPointsInPolygon

    isPointInCircleSector

    getPointsInCircleSector

    Thanks

    Monday, October 12, 2015 2:17 PM

Answers

  • You will unlikely find anything that is linq based for this. For the sector scenario you could start off with the logic you have for the getPointsInCircle to ensure the points are in the circle. After that you can calculate the heading from the center of the circle to the point to see if it in with the sector angles. Here is some code for calculating a heading from another project I have, but it shouldn't take much to get working in WP.

    /// <summary>
    /// Calculates the heading from one Coordinate to another.
    /// </summary>
    /// <param name="origin">Point of origin.</param>
    /// <param name="destination">Destination point to calculate relative heading to.</param>
    /// <returns>A heading degrees between 0 and 360. 0 degrees points due North.</returns>
    public static double CalculateHeading(Coordinate origin, Coordinate destination)
    {
    	double radianLat1 = ToRadians(origin.Latitude);
    	double radianLat2 = ToRadians(destination.Latitude);
    
    	double dLon = ToRadians(destination.Longitude - origin.Longitude);
    
    	double dy = Math.Sin(dLon) * Math.Cos(radianLat2);
    	double dx = Math.Cos(radianLat1) * Math.Sin(radianLat2) - Math.Sin(radianLat1) * Math.Cos(radianLat2) * Math.Cos(dLon);
    
    	return (ToDegrees(Math.Atan2(dy, dx)) + 360) % 360;
    }
    
    /// <summary>
    /// Converts an angle that is in degrees to radians. Angle * (PI / 180)
    /// </summary>
    /// <param name="angle">An angle in degrees</param>
    /// <returns>An angle in radians</returns>
    public static double ToRadians(double angle)
    {
    	return angle * (Math.PI / 180);
    }
    
    /// <summary>
    /// Converts an angle that is in radians to degress. Angle * (180 / PI)
    /// </summary>
    /// <param name="angle">An angle in radians</param>
    /// <returns>An angle in degrees</returns>
    public static double ToDegrees(double angle)
    {
    	return angle * (180 / Math.PI);
    }


    http://rbrundritt.wordpress.com

    Saturday, November 7, 2015 2:10 AM

All replies

  • Where do you want to use this code? If it is in a .NET app WPF/Console/service app (not windows 8/10/phone app), you can use the SQL spatial library in your code and do these calculations with really good spatial accuracy. If you are not using .NET, you can find some of the different calculations you are looking for. You can find a simple point in polygon algorithm here: http://www.codeproject.com/Tips/84226/Is-a-Point-inside-a-Polygon

    Not sure what you mean by get Points in Polygon, is this simple a loop of the point in polygon function against all shapes on the map?

    For a circle you can use either the point in polygon algorithm since a circle is just another type of polygon, you can use the haversine formula to calculate the distance from the center of the circle to each point and see if the distance is less than or equal to the radius of your circle.


    http://rbrundritt.wordpress.com

    Monday, October 12, 2015 5:38 PM
  • I suppose I should have provided more details.

    Yes .net but Windows Phone so no SQL Spatial.

    Re: getPointsInPolygon.  I was hoping to find something linq based if possible (rather than looping)

    Re: getPointsInCircleSector.  I can getPointsInCircle but how to get only the points in a sector (say from 25degrees to 120degrees)?

    Thanks

    Monday, October 12, 2015 6:02 PM
  • Any ideas on the Sector issue ?
    Friday, November 6, 2015 6:59 PM
  • You will unlikely find anything that is linq based for this. For the sector scenario you could start off with the logic you have for the getPointsInCircle to ensure the points are in the circle. After that you can calculate the heading from the center of the circle to the point to see if it in with the sector angles. Here is some code for calculating a heading from another project I have, but it shouldn't take much to get working in WP.

    /// <summary>
    /// Calculates the heading from one Coordinate to another.
    /// </summary>
    /// <param name="origin">Point of origin.</param>
    /// <param name="destination">Destination point to calculate relative heading to.</param>
    /// <returns>A heading degrees between 0 and 360. 0 degrees points due North.</returns>
    public static double CalculateHeading(Coordinate origin, Coordinate destination)
    {
    	double radianLat1 = ToRadians(origin.Latitude);
    	double radianLat2 = ToRadians(destination.Latitude);
    
    	double dLon = ToRadians(destination.Longitude - origin.Longitude);
    
    	double dy = Math.Sin(dLon) * Math.Cos(radianLat2);
    	double dx = Math.Cos(radianLat1) * Math.Sin(radianLat2) - Math.Sin(radianLat1) * Math.Cos(radianLat2) * Math.Cos(dLon);
    
    	return (ToDegrees(Math.Atan2(dy, dx)) + 360) % 360;
    }
    
    /// <summary>
    /// Converts an angle that is in degrees to radians. Angle * (PI / 180)
    /// </summary>
    /// <param name="angle">An angle in degrees</param>
    /// <returns>An angle in radians</returns>
    public static double ToRadians(double angle)
    {
    	return angle * (Math.PI / 180);
    }
    
    /// <summary>
    /// Converts an angle that is in radians to degress. Angle * (180 / PI)
    /// </summary>
    /// <param name="angle">An angle in radians</param>
    /// <returns>An angle in degrees</returns>
    public static double ToDegrees(double angle)
    {
    	return angle * (180 / Math.PI);
    }


    http://rbrundritt.wordpress.com

    Saturday, November 7, 2015 2:10 AM