Talk:Find the intersection of two lines: Difference between revisions
(added another re-formatted/re-written REXX version.) |
(added a new talk section.) |
||
Line 7: | Line 7: | ||
::* aligns the data points and results in the output |
::* aligns the data points and results in the output |
||
::* adds whitespace to make arithmetic computations more perusable |
::* adds whitespace to make arithmetic computations more perusable |
||
::* |
::* eschews title-case capitalization |
||
::* maintains the same line for the '''then''' clause and the '''if''' clause (no split statements) |
::* maintains the same line for the '''then''' clause and the '''if''' clause (no split statements) |
||
::* indentations for all REXX statements in the function |
::* indentations for all REXX statements in the function |
Revision as of 03:16, 19 May 2017
a REXX version of a REXX version
This REXX version is a re-write of version 2 of the REXX entry, with:
- aligned indentation for all do-end blocks (and encapsulated statements)
- elided superfluous zeroes and decimal points in (decimal) integers
- elides the superfluous and detracting use of concatenation (││)
- a unique symbol instead of a null literal for a special case
- aligns the data points and results in the output
- adds whitespace to make arithmetic computations more perusable
- eschews title-case capitalization
- maintains the same line for the then clause and the if clause (no split statements)
- indentations for all REXX statements in the function
- has the result on the same line as the input (data points)
- a different quoted literal style (for easier reading of multiple literals on the same clause)
- REXX variables to hold long literals that would otherwise cause excessive wide REXX statements
- a comma (instead of a slash) to separate the x y coördinates of the data points.
- elides superfluous do-end block structures
- a lot more whitespace within REXX statements and the REXX program's output
- tests all data possibilities (for showing all the tested non-intersecting conditions)
<lang rexx>/*REXX program finds (possibly) the intersection of two lines (with diagnostic errors).*/ say iSect( 4 0 6 10 0 3 10 7 ) say iSect( 0 0 0 10 0 3 10 7 ) say iSect( 0 0 0 10 0 3 10 7 ) say iSect( 0 0 0 1 1 0 1 7 ) say iSect( 0 0 0 0 0 3 10 7 ) say iSect( 0 0 3 3 0 0 6 6 ) say iSect( 0 0 3 3 0 1 6 7 ) say iSect( 0 0 3 3 8 8 8 8 ) exit /*stick a fork in it, we're all done. */ /*──────────────────────────────────────────────────────────────────────────────────────*/ iSect: procedure; parse arg xa ya xb yb xc yc xd yd
@ident= 'lines AB and CD are identical' /*literal to help shorten a line*/ @paral= 'lines AB and CD are parallel' /* " " " " " " */ $=. /*the interection (or error msg)*/ if xa=xb then do k1=. x1=xa if ya=yb then $= 'points A and B are identical' end else do k1=(yb-ya) / (xb-xa) d1=ya - k1 * xa end
if xc=xd then do k2=. x2=xc if yc=yd then $= 'points C and D are identical' end else do k2=(yd-yc) / (xd-xc) d2=yc - k2 * xc end
if $=. then do if k1=. then if k2=. then if x1=x2 then $=@ident else $=@paral else do x=x1 y=k2 * x + d2 end else if k2=. then do x=x2 y=k1 * x + d1 end else if k1=k2 then if d1=d2 then $= @ident else $= @paral else do x=(d2-d1) / (k1-k2) y=k1 * x + d1 end end
if $=. then $= 'intersection is at (' || x","y')' info= 'a=('xa","ya') b=('xb","yb') c=('xc","yc') d=('xd","yd')' return left(info, max(50, length(info) ) ) ' ───► ' $</lang>
- output when using the default input:
a=(4,0) b=(6,10) c=(0,3) d=(10,7) ───► intersection is at (5,5) a=(0,0) b=(0,10) c=(0,3) d=(10,7) ───► intersection is at (0,3) a=(0,0) b=(0,10) c=(0,3) d=(10,7) ───► intersection is at (0,3) a=(0,0) b=(0,1) c=(1,0) d=(1,7) ───► lines AB and CD are parallel a=(0,0) b=(0,0) c=(0,3) d=(10,7) ───► points A and B are identical a=(0,0) b=(3,3) c=(0,0) d=(6,6) ───► lines AB and CD are identical a=(0,0) b=(3,3) c=(0,1) d=(6,7) ───► lines AB and CD are parallel a=(0,0) b=(3,3) c=(8,8) d=(8,8) ───► points C and D are identical