question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to use center and lineArc to draw a curved line from PointA and PointB ?

See original GitHub issue

I’m using a Layer and a Feature from react-mapbox-gl to draw a line from point to point B` ( see screenshot 1)

My goal is to use line-arc and center from turf js in order to have a curved line between point A and point B

I managed to get a curved line, but it’s not starting at Point A and it’s not finishing at Point B… it is instead drawn in the middle of the map (see screenshot number 2)

Here is my code

     const features = turf.featureCollection([
       turf.point(this.state.startFlying),   // [-122.3811441, 37.6213129]
       turf.point(this.state.destinationFlying),  // [-118.4107187, 33.9415889]
     ]);

    const theCenter = turf.center(features);
    const arcRadius = 5;
    const bearing1 = 25;
    const bearing2 = 47;

    const arc = turf.lineArc(theCenter, arcRadius, bearing1, bearing2);
    const mappedCurvedFlyingCurve = arc.geometry.coordinates;

screen shot 2018-01-08 at 1 17 16 pm

screen shot 2018-01-08 at 1 22 10 pm

screen shot 2018-01-08 at 1 22 31 pm

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:13 (3 by maintainers)

github_iconTop GitHub Comments

24reactions
savy-91commented, Feb 28, 2020

Here is how I did it:

drawArc(id: string, start: UniversalPlace, finish: UniversalPlace) {
    let route: LineString = {
      type: 'LineString',
      coordinates: [
        [start.longitude, start.latitude],
        [finish.longitude, finish.latitude]
      ]
    };
    route = projection.toWgs84(route);
    const lineD = lineDistance(route, {units: 'kilometers'});
    const mp = midpoint(route.coordinates[0], route.coordinates[1]);
    const center = destination(
      mp,
      lineD,
      bearing(route.coordinates[0], route.coordinates[1]) - 90
    );
    const lA = lineArc(
      center,
      distance(center, route.coordinates[0]),
      bearing(center, route.coordinates[1]),
      bearing(center, route.coordinates[0])
      );
    this.mapView.addLayer(
      {
        id,
        type: 'line',
        source: {
          type: 'geojson',
          data: projection.toMercator(lA)
        },
        paint: {
          'line-width': 4,
          'line-color': '#FCA526'
        }
      }
    );
  }

Example of the result: image

6reactions
stebogitcommented, Jun 30, 2021

@crevulus I believe the issue is, once again, projection/spherical distortion, i.e. working with euclidean (plane) distance and bearing instead of Rhumb functions. In your case the distances are so big, compared to @savy-91’s example, that the final result is way off.

Look at this example, I believe it works much better, although still not 100% accurate:

const start = [-118.30078125, 33.925129700072]; // LA
const end = [-157.6318359375, 20.92039691397189]; // Hawaii
// rhumb functions
const radius = turf.rhumbDistance(start, end);
const midpoint = turf.midpoint(start, end);
const bearing = turf.rhumbBearing(start, end) - 89; // <-- not 90˚
const origin = turf.rhumbDestination(midpoint, radius, bearing);

const curvedLine = turf.lineArc(
  origin,
  turf.distance(origin, start),
  turf.bearing(origin, end),
  turf.bearing(origin, start),
  { steps: 128 }
);

Screen Shot 2021-06-29 at 5 07 48 PM

Read more comments on GitHub >

github_iconTop Results From Across the Web

Formula or Algorithm to Draw curved lines between points
Ullrich I'm not wanting to use Bezier Curves because I need the line to "touch" the point and the Bezier Curve will just...
Read more >
Draw a curve or circle shape - Microsoft Support
Draw a curve. On the Insert tab, click Shapes. Under Lines, click Curve. Click where you want the curve to start, drag to...
Read more >
OST - 06.04.01 Drawing Arcs (Curved Linear Segments)
Using any Linear Condition , draw a line dissecting the circle ... By adjusting the end points and the center point, you can...
Read more >
Draw a curved line with given radius, and two locations
I found a solution for my question. What I did was. Get the distance between the two points (current location and next location) ......
Read more >
About Curved Objects | AutoCAD 2022
Arcs--You can create arcs by specifying any three points. · Circles--You can create circles by specifying the center and the radius. · Polyline ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found