Android Question drawing an arc with 2 points and a ray length

DALB

Active Member
Licensed User
Hi,

Looking at the different forums, I see that it's possible to draw an arc knowing the starting angle and the ending angle.
But, has someone developed a little code which permits to draw an arc only with 2 points and the radius ?

for instance:
On a map (the flat eath with any projection) I define 2 points. The code draw the arc with the radius (on one side of the line joining the 2 points).

I find something like this

drawArc2points:
sub DrawArc2points(x1 as int,y1 as int,x2 as int,y2 as int,radius a int, side as boolean)
....
end sub

??? a part of my message was cleared !!!!

x1,y1,x1,y2 : positions of the 2 points
radius, radieus of the arc
side, the side in which the arc has to be drawn (laft.right,up,down) regarding to the line which joins the 2 points.

if there is no answer, I'll develope the code or adapt one.
 

Attachments

  • map22.zip
    279.3 KB · Views: 131

DonManfred

Expert
Licensed User
Longtime User
Why you are creating a second thread for the same issue?

 
Upvote 0

DALB

Active Member
Licensed User
wow, I didn't see this !
Maybe, I didn;t make care with the register button ! so, don't mind, I can delete one one the two.
 
Upvote 0

William Lancee

Well-Known Member
Licensed User
Longtime User
Is this what you would like?

B4X:
Sub Class_Globals
    Type point(x As Float, y As Float)
    Private Root As B4XView
    Private xui As XUI
    Private cv As B4XCanvas
End Sub

Public Sub Initialize
End Sub

Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    cv.Initialize(Root)

    Dim pt1 As point = NewPoint(Root.width / 4, Root.Height / 3)
    Dim pt2 As point = NewPoint(Root.Width - Root.width / 4, Root.Height / 2)
    drawArc2(250, pt1, pt2, True, False, False)
    
    'Relative to origin
    
End Sub

Private Sub drawArc2(radius As Float, pt1 As point, pt2 As point, hide1 As Boolean, hide2 As Boolean, flip As Boolean)
    'find origin, point where distance(origin, pt1) = distance(origin, pt2) = radius
    'this is the intersection of 2 arcs (center as pt1) and (center as pt2)
    'https://math.stackexchange.com/a/1033561

    'There are two solutions to this, controlled by flip
    Dim z As Float = -1
    If flip Then z = 1
    Dim d As Float = distance(pt1, pt2)
    Dim m As Float = d / 2
    Dim delta As Float = radius * radius - m * m
    If delta < 0 Then
        Log("radius < half the distance between pt1 and pt2, two arcs do not intersect")
        Return
    End If

    Dim h As Float = Sqrt(delta)                
    Dim x As Float = (m / d) * (pt2.x - pt1.x) + z * (h / d) * (pt2.y - pt1.y) + pt1.x
    Dim y As Float = (m / d) * (pt2.y - pt1.y) - z * (h / d) * (pt2.x - pt1.x) + pt1.y
    Dim origin As point = NewPoint(x, y)

    'get co-ordinates relative to origin
    Dim p1 As point = NewPoint(pt1.x - origin.x, pt1.y - origin.y)
    Dim p2 As point = NewPoint(pt2.x - origin.x, pt2.y - origin.y)

    'check to see if all is good
    cv.DrawCircle(origin.x + p1.x, origin.y + p1.y, 5, xui.Color_Blue, False, 1)
    cv.DrawCircle(origin.x + p2.x, origin.y + p2.y, 5, xui.Color_Red, False, 1)
    cv.DrawCircle(origin.x, origin.y, 5, xui.Color_Green, False, 1)
    
    Dim angle1 As Float = ATan2D(p1.y, p1.x)
    Dim angle2 As Float = ATan2D(p2.y, p2.x)
    Dim sweep As Float = angle2 - angle1
    
    Dim arc As B4XPath
    arc.InitializeArc(origin.x, origin.y, radius, angle1, sweep)
    cv.DrawPath(arc, xui.Color_Black, False, 1)
    
    If hide1 Then cv.DrawLine(pt1.x, pt1.y, origin.x, origin.y, xui.Color_White, 3)
    If hide2 Then cv.DrawLine(pt2.x, pt2.y, origin.x, origin.y, xui.Color_White, 3)
End Sub

Private Sub distance(pt1 As point, pt2 As point) As Float
    Return Sqrt((pt1.x - pt2.x) * (pt1.x - pt2.x) + (pt1.y - pt2.y) * (pt1.y - pt2.y))
End Sub

Public Sub NewPoint(x As Float, y As Float) As point
    Dim t1 As point
    t1.Initialize
    t1.x = x
    t1.y = y
    Return t1
End Sub
 
Last edited:
Upvote 0

DALB

Active Member
Licensed User
Thanks much Will. I've written something close to your code, but using a type for the points as you do is clever and efficient. I didn't thought about this.
It works well.
Now, I have to find the conversion matrix which permits to convert the shorter distance between two cities on the world on the sphere our Earth is to this arc (for instance Tokyo and The Cap in south Africa.). That is to say, finding the correct radius of the arc.

Thanks much again Will.
 
Upvote 0

William Lancee

Well-Known Member
Licensed User
Longtime User
The circumference of the earth is close to 40,070 km.

You can use the sweep angle in the above Sub to compute distance along the arc (which is not called radius).
Say the sweep angle is 54 degrees. This is 15% of 360. Therefore the length of the defined arc is 15% of 40,070 km.
Or 6,010 km.

If the sweep angle is greater 180 degrees, subtract it from 360 to get the shorter arc.

Make sure the computed origin is the center of the earth and not the one in outer space. Flip it otherwise.
 
Last edited:
Upvote 0

William Lancee

Well-Known Member
Licensed User
Longtime User
The distance between Tokyo and Cape town is 14,718 km.
Which is 36.7 % of 40,070 km.
Therefore the sweep angle will be approximately .367 * 360 = 132 degrees.
 
Upvote 0

derez

Expert
Licensed User
Longtime User
Not sure what you realy want but:
1. GPS library includes a Location object which can calculate distance and bearing between two point. The results are of the arc which is part of a Great Circle, which means that it is the shortest route.
2. The same results are present in Navigation Library https://www.b4x.com/android/forum/threads/16334/#content

If you want to draw the arc on a map (which is flat of course) check the bearing from point 1 to point 2 and the bearing from 2 to 1, they will be different and show you the two edges of the arc.
 
Upvote 0

DALB

Active Member
Licensed User
Hi,

Thank you you two. I'll have a look at the library.
Yes Will, I think this calculation gives me an approx distance, which is fine.
Derez, thanks for the idea of the library.
You two, ith your ideas I'll have a look, I think able to do what I want.
After different tests, I'll come back giving an appreciation.
 
Upvote 0
Top