Python - How to find x and y coordinates of a circle to create an arc on a map

I am writing a python script that will display some points and warp on a map based on the data I receive. I am trying to get the coordinates of the center of an arc / circle using any of the data I get below. It's hard for me to wrap my brain around me.

  • Point1 Lat and Lon
  • Point2 Lat and Lon
  • Start angle
  • Corner angle
  • Circle radius

I understand that I don't need all this data to solve this problem.

I also have a flag that tells me if his left or right hand is turning.

It turns out that the roots x and y can be calculated using the information I get above.

I already have a function that runs in javascript to draw an arc.

function drawArc(center, initialBearing, finalBearing, radius) {{
    var d2r = Math.PI / 180;   // degrees to radians 
    var r2d = 180 / Math.PI;   // radians to degrees 

    var points = 32; 

    // find the raidus in lat/lon 
    var rlat = (radius / EarthRadiusMeters) * r2d;
    var rlng = rlat / Math.cos(center.lat() * d2r); 

    var extp = new Array();

    if (initialBearing > finalBearing) finalBearing += 360;
    var deltaBearing = finalBearing - initialBearing;
    deltaBearing = deltaBearing/points;
    for (var i=0; (i < points+1); i++) 
    {{ 
      extp.push(center.DestinationPoint(initialBearing + i*deltaBearing, radius)); 
      bounds.extend(extp[extp.length-1]);
    }}
    return extp;
}}

      

I can't let my life find a good way to get the x and y coordinates of the center of the circle.

enter image description here

+3


source to share


2 answers


Given a point (Px, Py), this starting angle (Pa) and radius (r), you can calculate the center (Cx, Cy) like this:

Cx = Px - r * cos(Pa)
Cy = Py - r * sin(Pa)

      

For example, if you want the center of an arc with a point at point (1, 2), which is Pi / 3 radians (60 degrees) in a circle with a radius of 3, you can calculate the center like this:



Cx = 1 - 3 * cos(Pi/4) = 1 - 3 * 0.5 = 1 - 1.5 = -0.5
Cy = 1 - 3 * sin(Pi/4) = 1 - 3 * sqrt(2)/2 = -1.1213
C = (-0.5, -1.1213)

      

There are similar formulas that you could use with other information. For example, you could calculate the center based on the "first" and "second" points, radius, and "direction of rotation" (left or right). This is a fairly simple trigonometry for deriving these equations.

0


source


AFAIK, the center point is independent of the circle parameters. These are the XY coordinates on the canvas. It can be anywhere or more often in the middle of the image to look better.

Look at here:



#!/usr/bin/python
# -*- coding: utf-8 -*-

import cairo, math

radius=100

starting_angle = math.radians(210) #  angle in radians
ending_angle = math.radians(330)

WIDTH, HEIGHT = radius*2+20, radius*2+20 # Canvas size

x=WIDTH/2 # X position
y=HEIGHT/2 # Y position


surface = cairo.ImageSurface (cairo.FORMAT_ARGB32, WIDTH, HEIGHT)
context = cairo.Context (surface)

# Background Neutral Gray ----------
context.set_source_rgb(0.25, 0.25, 0.25)
context.rectangle(0, 0, WIDTH, HEIGHT)
context.fill()


# Red Sector -------------------
context.set_source_rgb(1, 0, 0) # rgb color red
context.arc(x, y, radius, starting_angle, ending_angle)
context.line_to (x, y)
context.fill()


surface.write_to_png ("img.png") # Output to PNG

      

enter image description here

0


source







All Articles