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