Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

formula to calculate bounding coordinates of an arc in space

Tags:

math

formula

I have 2 lines that intersect at a point with know coordinates - x1,y1 - x2,y2 - x3,y3

From this I have calculated an arc at a given radius between the lines. So I now know - 2 arc endpoints x4,y4 and x5,y5 - arc centrepoint Cx,Cy - arc radius r - starting and ending angles relative to the X axis in polar and therefore the angle between the lines.

bounding coordinates of an arc in space

I want to create a formula that will calculate the maximum and minimum X and Y values of the arc. I.e. the coordinates of the box that would enclose the arc.

In the example below I can find out the minimum X value and maximum Y value, they are known values, but am unsure how to calculate the maximum X and minimum Y.

In other instances the arc could be any coordinates so the known minimum and maximum values will change.

I know how to calculate points along an arc at a given angle or intervals but not the maximum and minimum in a particular direction, in this case X and Y axis.

I am going to use the formula in programming.

like image 438
Mark Avatar asked Feb 09 '23 03:02

Mark


1 Answers

Suppose we have start angle θ1, end angle θ2 (both in radians), radus r, direction of the arc counterclockwise. We'd like to find Xmax,Ymax,Xmin and Ymin. Consider this values as functions of quadrants q=f(θ):

Xmax=f(q1,q2,r), Ymax=f(q1,q2,r), Xmin=f(q1,q2,r), Ymin=f(q1,q2,r).

Instead of writing huge number of "if" statements it's convenient to represent this functions as an "extremum matrices". Evaluating functions f(q1,q2,r) we'll end up with this matrices.

So here is the algorithm:

  1. Find quadrants (q1,q2) of θ1 and θ2;
  2. Convert θ1,θ2,r to the Cartesian coordinates;
  3. Find bounding box excluding extreme points;
  4. Build extremum matrices;
  5. Select Xmax,Ymax,Xmin,Ymin accoding to q1 and q2 from this matrices.

Here's my C#6 implementation:

using System;
using System.Windows;
using static System.Math;

public static class GeomTools
{
    public static Byte GetQuadrant(this Double angle)
    {
        var trueAngle = angle%(2*PI);
        if (trueAngle >= 0.0 && trueAngle < PI/2.0)
            return 1;
        if (trueAngle >= PI/2.0 && trueAngle < PI)
            return 2;
        if (trueAngle >= PI && trueAngle < PI*3.0/2.0)
            return 3;
        if (trueAngle >= PI*3.0/2.0 && trueAngle < PI*2)
            return 4;
        return 0;
    }
    public static Rect GetBounds(Double startAngle, Double endAngle, Double r)
    {
        var startQuad = startAngle.GetQuadrant() - 1;
        var endQuad = endAngle.GetQuadrant() - 1;

        // Convert to Cartesian coordinates.
        var stPt = new Point(Round(r*Cos(startAngle), 14), Round(r*Sin(startAngle), 14));
        var enPt = new Point(Round(r*Cos(endAngle), 14), Round(r*Sin(endAngle), 14));

        // Find bounding box excluding extremum.
        var minX = stPt.X;
        var minY = stPt.Y;
        var maxX = stPt.X;
        var maxY = stPt.Y;
        if (maxX < enPt.X) maxX = enPt.X;
        if (maxY < enPt.Y) maxY = enPt.Y;
        if (minX > enPt.X) minX = enPt.X;
        if (minY > enPt.Y) minY = enPt.Y;

        // Build extremum matrices.
        var xMax = new[,] {{maxX, r, r, r}, {maxX, maxX, r, r}, {maxX, maxX, maxX, r}, {maxX, maxX, maxX, maxX}};
        var yMax = new[,] {{maxY, maxY, maxY, maxY}, {r, maxY, r, r}, {r, maxY, maxY, r}, {r, maxY, maxY, maxY}};
        var xMin = new[,] {{minX, -r, minX, minX}, {minX, minX, minX, minX}, {-r, -r, minX, -r}, {-r, -r, minX, minX}};
        var yMin = new[,] {{minY, -r, -r, minY}, {minY, minY, -r, minY}, {minY, minY, minY, minY}, {-r, -r, -r, minY}};

        // Select desired values
        var startPt =new Point(xMin[endQuad, startQuad], yMin[endQuad, startQuad]);
        var endPt=new Point(xMax[endQuad, startQuad], yMax[endQuad, startQuad]);
        return new Rect(startPt,endPt);
    }
}

It is fair for arc centre point at (0,0) but you can easily move resulting bounding box to your Cx,Cy.

Unlike Tim Buegeleisen's approximate solution this solution is exact, though it may be a little bit more memory expensive.

like image 183
Oleg Petrochenko Avatar answered Feb 11 '23 18:02

Oleg Petrochenko