(42S) Determine Circle From Three Given Points - Printable Version +- HP Forums (https://www.hpmuseum.org/forum) +-- Forum: HP Software Libraries (/forum-10.html) +--- Forum: General Software Library (/forum-13.html) +--- Thread: (42S) Determine Circle From Three Given Points (/thread-11056.html) |
(42S) Determine Circle From Three Given Points - gerry_in_polo - 07-15-2018 08:43 AM 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. RE: (42S) Determine Circle From Three Given Points - Dieter - 07-15-2018 04:38 PM (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 RE: (42S) Determine Circle From Three Given Points - gerry_in_polo - 07-16-2018 12:49 AM 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. RE: (42S) Determine Circle From Three Given Points - gerry_in_polo - 07-16-2018 01:19 AM Another observation: your program is 79 lines long, mine is 87, not really a significant difference although a different approach is always worth knowing. RE: (42S) Determine Circle From Three Given Points - Dieter - 07-16-2018 07:19 AM (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 RE: (42S) Determine Circle From Three Given Points - Thomas Klemm - 07-17-2018 01:21 PM 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 RE: (42S) Determine Circle From Three Given Points - Dieter - 07-17-2018 07:17 PM (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 |