Colinear Points - Printable Version +- HP Forums (https://www.hpmuseum.org/forum) +-- Forum: HP Calculators (and very old HP Computers) (/forum-3.html) +--- Forum: HP Prime (/forum-5.html) +--- Thread: Colinear Points (/thread-236.html) |
Colinear Points - Terje Vallestad - 12-25-2013 04:48 PM Following up on a HP28s example made by Damien in another sub-forum, I thought to try to do the same on the Prime. Damien's examples and explanation: A(a, a'), B(b, b'), C(c, c') are lined up if the determinant = 0 |a a' 1| |b b' 1| = 0 |c c' 1| Examples: (2,2) (4,1) (5,3) -> The points are not colinear (2,-3) (4,1) (5,3) -> The points are colinear Code:
The program must be run in Textbook or Algebraic as if not the calculator will crash, so to avoid that I preserve the Entry mode in a local variable and reinstate it at the end. It is also only relevant to 3 points and does not check if more points have been entered in the matrix editor. However, when running the program off the stack whilst in RPN mode it always return a 2 (which is the Entry state) to the stack. I have looked but cannot find a way to avoid this undesired result in the program. Does anybody have a suggestion for how this could be achieved? Cheers, Terje RE: Colinear Points - Han - 12-25-2013 05:33 PM Thanks for sharing! Quick question, though: Why check if 1/det(...) produces an error, as opposed to simply checking if det(...) is non-zero? Code:
RE: Colinear Points - debrouxl - 12-25-2013 06:13 PM You can also use the (x3 - x1) * (y2 - y1) + (y3 - y1) * (x1 - x2) formula for computing collinearity without det() or even a matrix. 4 subtractions, 2 multiplications and 1 addition make this formula highly suitable to implementation on embedded platforms. (just looked up the definition again on the Internet, I seldom use it and never remember it ) RE: Colinear Points - Terje Vallestad - 12-25-2013 06:56 PM (12-25-2013 05:33 PM)Han Wrote: Thanks for sharing! Quick question, though: Why check if 1/det(...) produces an error, as opposed to simply checking if det(...) is non-zero? I'm probably a glass half empty man rather than half full...... :-) However, I needed a trigger/trap and ISERR was the first one I found in the manual. Being new to programming PPL I did not look further. Would it in your opinion be a benefit to look for a positive rather than a negative in this instance? Thanks, Terje RE: Colinear Points - Terje Vallestad - 12-25-2013 07:05 PM (12-25-2013 06:13 PM)debrouxl Wrote: You can also use the (x3 - x1) * (y2 - y1) + (y3 - y1) * (x1 - x2) formula for computing collinearity without det() or even a matrix. 4 subtractions, 2 multiplications and 1 addition make this formula highly suitable to implementation on embedded platforms. Thanks Debroux, To me this was more about writing my first PPL programme rather than the maths. Given the speed of the Prime and the relative ease of entering a matrix I thought using a matrix was quite a quick way of entering the required input? That said, how would you go about the programme using the formula? Using INPUT? I'm certainly here to learn. Cheers, Terje RE: Colinear Points - patrice - 12-25-2013 07:47 PM Your program can be made a function. Code: EXPORT COLINEAR(Mt) RE: Colinear Points - Terje Vallestad - 12-25-2013 09:35 PM (12-25-2013 07:47 PM)patrice Wrote: Your program can be made a function. Thanks Patrice, No bells and whistles, but very neat. Also gets rid of my problem with a return value I didn't need. Cheers, Terje RE: Colinear Points - Han - 12-26-2013 01:59 AM (12-25-2013 06:56 PM)Terje Vallestad Wrote:(12-25-2013 05:33 PM)Han Wrote: Thanks for sharing! Quick question, though: Why check if 1/det(...) produces an error, as opposed to simply checking if det(...) is non-zero? IFERR would be a perfectly fine solution if the only error produced from 1/det(...) is a division by zero error. However, there are other ways det(...) could produce an error (non-square matrix when the user accidentally creates more entries than necessary). So an error having nothing to do with colinearity would incorrectly result in the wrong conclusion of "colinear." To make the program exit gracefully when an error occurs, you may want to test the dimensions of the matrix prior to executing det(...). |