(42S) Determine Circle From Three Given Points
|
07-15-2018, 08:43 AM
Post: #1
|
|||
|
|||
(42S) Determine Circle From Three Given Points
Calculates the center and radius of the circle passing through three given points. Example, enter X1=1, Y1=4, X2=-1, Y2=2, X3=4, Y3=-3. Result, center (Xc,Yc)=(2.5,0.5) and radius R=3.8079.
|
|||
07-15-2018, 04:38 PM
(This post was last modified: 07-15-2018 04:45 PM by Dieter.)
Post: #2
|
|||
|
|||
RE: (42S) Determine Circle From Three Given Points
(07-15-2018 08:43 AM)gerry_in_polo Wrote: Calculates the center and radius of the circle passing through three given points. Example, enter X1=1, Y1=4, X2=-1, Y2=2, X3=4, Y3=-3. Result, center (Xc,Yc)=(2.5,0.5) and radius R=3.8079. Thank you very much for your program. This problem, solving a circle through three given points, reminds me of the early days with my first programmable calculator, a 34C. I had a HP67/97 book which included auch a program, and I tried to adapt it for the 34C. Essentially the problem boils down to solving a linear equation system with three unknowns. In matrix notation it can be written this way: Code: (x1 y1 -1) (2*xc ) (x1² + y1²) Since the 42s features matrix support, this approach can be implemented in a quite compact program. So here is my alternative solution: Code: 00 { 184-Byte Prgm } Note: "|-" means "append", "^" is "↑" and "\LF" is a line feed. Your example: Code: XEQ "CIRCLE" Since I am not very familiar with 42s matrix programming the program can probably be improved with more efficient code. So if someone can provide a better version: go ahead. Dieter |
|||
07-16-2018, 12:49 AM
Post: #3
|
|||
|
|||
RE: (42S) Determine Circle From Three Given Points
The matrix approach does not readily illustrate the basic principle of determining the circle which encompasses three points: the intersection of the two perpendicular inspectors of the two chords linking the first and second and then the second and third given points as the circle's center and subsequent computation of the radius.
|
|||
07-16-2018, 01:19 AM
Post: #4
|
|||
|
|||
RE: (42S) Determine Circle From Three Given Points
Another observation: your program is 79 lines long, mine is 87, not really a significant difference although a different approach is always worth knowing.
|
|||
07-16-2018, 07:19 AM
(This post was last modified: 07-16-2018 11:55 AM by Dieter.)
Post: #5
|
|||
|
|||
RE: (42S) Determine Circle From Three Given Points
(07-16-2018 01:19 AM)gerry_in_polo Wrote: Another observation: your program is 79 lines long, mine is 87, not really a significant difference ... Your program can even be shorter by using RCL-arithmetic. On the other hand my version has a quite elaborate output routine. Line 65 ff. can be replaced with a simple Code: 65 VIEW "XC" Then take a look at the byte count. With the above simplification it's down to 149 Bytes. (07-16-2018 01:19 AM)gerry_in_polo Wrote: ...although a different approach is always worth knowing. Yes, that's exactly why I posted it. This is just another way of solving the same problem. Isn't this what forums are all about? But let me explain why I chose this method: The approach here is the general circle equation: (X–Xc)² + (Y–Yc)² = R². The program solves the resulting equation system for the three given points. There is one significant advantage of this method: it doesn't matter if two Y-values are the same. Try (0|2), (4|2) and (2|0) and the program correctly returns the center at (2|2) and a radius of 2. This will not work with the other approach where a division by (Y2–Y1) and (Y3–Y2) takes place. Dieter |
|||
07-17-2018, 01:21 PM
Post: #6
|
|||
|
|||
RE: (42S) Determine Circle From Three Given Points
The HP-42S allows to use complex numbers to solve the problem.
Formula The three complex number \(a\), \(b\) and \(c\) represent the vertices of the triangle. Define the following: \[ \begin{eqnarray} p=c+a \\ P=c-a \\ \\ q=a+b \\ Q=a-b \end{eqnarray} \] Then for the center \(z\) of the circle the following holds true: (The \(\cdot\) is for the dot-product.) \[ \begin{eqnarray} P\cdot(z-\frac{p}{2})=0 \\ Q\cdot(z-\frac{q}{2})=0 \end{eqnarray} \] This means that the vector from the center to the midpoint is perpendicular to the sides of the triangle. Given: \[ \begin{eqnarray} u=P\cdot p \\ v=Q\cdot q \end{eqnarray} \] We can rewrite these equations: \[ \begin{eqnarray} P\cdot z=P\cdot\frac{p}{2}=\frac{u}{2} \\ Q\cdot z=Q\cdot\frac{p}{2}=\frac{v}{2} \end{eqnarray} \] Or then: \[ \begin{eqnarray} P\cdot 2z=u \\ Q\cdot 2z=v \end{eqnarray} \] Using: \[ \begin{eqnarray} P=P_x+iP_y \\ Q=Q_x+iP_y \\ \\ z=x+iy \\ w=v+iu \end{eqnarray} \] This equation can be written using matrix notation: \[ \begin{bmatrix} P_x & P_y \\ Q_x & Q_y \end{bmatrix} \begin{bmatrix} 2x \\ 2y \end{bmatrix} = \begin{bmatrix} u \\ v \end{bmatrix} \] If we transpose this matrix we can rewrite the equation as: \[ \begin{eqnarray} g=Q_x+iP_x \\ h=Q_y+iP_y \\ \\ 2x\cdot g+2y\cdot h=w \end{eqnarray} \] This equation can be cross multiplied both by \(h\) and \(g\): (Reminder: \(h\times h=g\times g=0\).) \[ \begin{eqnarray} 2x\cdot h\times g=h\times w \\ 2y\cdot h\times g=w\times g=-g\times w \end{eqnarray} \] The determinant is: \[ D=h\times g \] We end up with: \[ \begin{eqnarray} x=\frac{h\times w}{2D} \\ y=-\frac{g\times w}{2D} \end{eqnarray} \] Program Code: 00 { 69-Byte Prgm } ; X Y Z T The radius of the circle isn't calculated. But that should be easy now that we know the center. Example: A = (1, 4) B = (-1, 2) C = (4, -3) 1 ENTER 4 COMPLEX -1 ENTER 2 COMPLEX 4 ENTER -3 COMPLEX XEQ "CENTER" 2.5 i0.5 Kind regards Thomas |
|||
07-17-2018, 07:17 PM
Post: #7
|
|||
|
|||
RE: (42S) Determine Circle From Three Given Points
(07-17-2018 01:21 PM)Thomas Klemm Wrote: The HP-42S allows to use complex numbers to solve the problem. Hey, that's a really great new approach! Hats off! (07-17-2018 01:21 PM)Thomas Klemm Wrote: The radius of the circle isn't calculated. But that should be easy now that we know the center. The simplest way of calculating the radius may be storing one of the points (as a complex number) at the beginning (insert STO "P" between line 01 and 02), and add a final ENTER RCL–"P" ABS at the end. This will return the center coordinates in Y and the radius in X: 1 ENTER 4 COMPLEX -1 ENTER 2 COMPLEX 4 ENTER -3 COMPLEX XEQ "CENTER" => 2,5 i0,5 3,80788655... Dieter |
|||
« Next Oldest | Next Newest »
|
User(s) browsing this thread: 6 Guest(s)