Post Reply 
Colinear Points
12-25-2013, 04:48 PM
Post: #1
Colinear Points
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:

EXPORT COLINEAR()

BEGIN

// Declare local variable to hold matrix and state
LOCAL Points,s;

// Preserve entry mode and set to textbook
s:=Entry;
Entry:=0;

// Create input matrix 3 rows by 2 cols
Points:=[[0,0],[0,0],[0,0]];

// Allow input of points and add column of one's to make square
EDITMAT(Points);
ADDCOL(Points,[1,1,1],3);

// Check if determinant is equal to 0 and display result in MSGBOX
IFERR 1/det(Points) 
THEN MSGBOX("The points are colinear") 
ELSE MSGBOX("The points are not colinear") 
END;

// Reinstate entry mode
Entry:=s;
END;

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
Find all posts by this user
Quote this message in a reply
12-25-2013, 05:33 PM
Post: #2
RE: Colinear Points
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:

IF det(Points) 
THEN MSGBOX("The points are not colinear") 
ELSE MSGBOX("The points are colinear") 
END;

Graph 3D | QPI | SolveSys
Find all posts by this user
Quote this message in a reply
12-25-2013, 06:13 PM
Post: #3
RE: Colinear Points
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 Smile )
Find all posts by this user
Quote this message in a reply
12-25-2013, 06:56 PM
Post: #4
RE: Colinear Points
(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
Find all posts by this user
Quote this message in a reply
12-25-2013, 07:05 PM
Post: #5
RE: Colinear Points
(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.

(just looked up the definition again on the Internet, I seldom use it and never remember it Smile )

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
Find all posts by this user
Quote this message in a reply
12-25-2013, 07:47 PM
Post: #6
RE: Colinear Points
Your program can be made a function.
Code:
EXPORT COLINEAR(Mt)
BEGIN
// Declare local variable to hold matrix and state
LOCAL Tmp;
// add column of one's to make square
ADDCOL(Mt,[1,1,1],3);
// Check if determinant is equal to 0
IF det(Mt) == 0 THEN Tmp:= 1; ELSE Tmp:= 0; END;
RETURN Tmp;
END;
so that you can call it from Home or anotherr program.

Patrice
“Everything should be made as simple as possible, but no simpler.” Albert Einstein
Find all posts by this user
Quote this message in a reply
12-25-2013, 09:35 PM
Post: #7
RE: Colinear Points
(12-25-2013 07:47 PM)patrice Wrote:  Your program can be made a function.
Code:
EXPORT COLINEAR(Mt)
BEGIN
// Declare local variable to hold matrix and state
LOCAL Tmp;
// add column of one's to make square
ADDCOL(Mt,[1,1,1],3);
// Check if determinant is equal to 0
IF det(Mt) == 0 THEN Tmp:= 1; ELSE Tmp:= 0; END;
RETURN Tmp;
END;
so that you can call it from Home or anotherr program.

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
Find all posts by this user
Quote this message in a reply
12-26-2013, 01:59 AM
Post: #8
RE: Colinear Points
(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?

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

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(...).

Graph 3D | QPI | SolveSys
Find all posts by this user
Quote this message in a reply
Post Reply 




User(s) browsing this thread: 1 Guest(s)