Page 1 of 1

calculate center of arc

Posted: Wed May 19, 2021 8:35 am
by ludoke
I've been trying for a while to find a way to calculate the center of an arc, but I can't get out, maybe there's a mathematical genius.
I have the startpoint X1,Y1 the endpoint X2,Y2 and the radius R ,I need the centerpoint XC,YC .
The arc can be in one or more quadrants.

Re: calculate center of arc

Posted: Wed May 19, 2021 9:26 am
by STARGÅTE
As you probably can imagine, there are two solution for such a center point:
https://stackoverflow.com/questions/429 ... orrespondi

Re: calculate center of arc

Posted: Wed May 19, 2021 10:39 am
by infratec
In PB:

Code: Select all

EnableExplicit


Procedure.i Circle2PtsRad(x1.i, y1.i, x2.i, y2.i, r.i, *middle.Point)
  
  Protected Result.i, d2.d, det.d, h.d
  
  
  d2 = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)
  det = r * r / d2 - 0.25
  
  If det >= 0.0
    h = Sqr(det)
    *middle\x = (x1 + x2) * 0.5 + (y1 - y2) * h
    *middle\y = (y1 + y2) * 0.5 + (x2 - x1) * h
    Result = #True
  EndIf
  
  ProcedureReturn Result
  
EndProcedure


Define Middle.Point

#x1 = 80
#y1 = 30

#x2 = 130
#y2 = 30

#r = 80

If OpenWindow(0, 0, 0, 200, 200, "CircleCalc", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  If CreateImage(0, 200, 200) And StartDrawing(ImageOutput(0))
    
    If Circle2PtsRad(#x1, #y1, #x2, #y2, #r, @Middle)
      Debug Str(Middle\x) + " / " + Str(Middle\y)
      
      Circle(Middle\x, Middle\y, #r, #Blue)
      
      Plot(#x1, #y1, #Red)
      Plot(#x2, #y2, #Red)
      
    Else
      Debug "Not possible"
    EndIf
    
    StopDrawing() 
    ImageGadget(0, 0, 0, 200, 200, ImageID(0))
  EndIf
  
  Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf

Re: calculate center of arc

Posted: Wed May 19, 2021 11:08 am
by IdeasVacuum
.... We had a lot of trigonometry examples in the Games Math section - but that seems to have disappeared when the new forum was introduced?

Re: calculate center of arc

Posted: Wed May 19, 2021 12:44 pm
by Olli
@ludoke

I agree with STARGÅTE : there are 2 points which match to the initial characteristics.

But what it misses is only a binary status. More accurately, in geometry, what it misses is an orientation.

The tip of infratec consists in deciding, on computing, the characteristics (points positions) have an order.

Point1, point2, etc...

While, in math, this "communicating flow order" does not exist :

PointA, PointB, etc... : while we do not add features in math, every points (or generally, every objects) can be exchangeable (PointA should welly be called PointB, etc...). That is the reason, in math, it misses an information, which is here binary (2 stats). Information which did not seem disturb infratec.
IdeasVacuum wrote:.... We had a lot of trigonometry examples in the Games Math section - but that seems to have disappeared when the new forum was introduced?
we can try to ask this in the bug report to understand...

Math remark for ludoke : I was myself on the point that from 2 two points and from one radius value, as you asked, there were an infinity of results, because there are several manner to describe an arc :

- circular arc
- elliptic arc
- rotated elliptic arc

I was far to answer you...

Re: calculate center of arc

Posted: Wed May 19, 2021 8:32 pm
by ludoke
infratec thanks,I know that there are 2 solutions.
Now I can try to find the start and end angle of the arc.
Maybe I can ask you this, too.