Jump to content

Talk:Find the intersection of two lines: Difference between revisions

m
→‎a REXX version of a REXX version: added/changed wording in the REXX section header, formatted the REXX program logic to be able to be viewed on a single screen.
m (added a missing verb (uses indentatations ...).)
m (→‎a REXX version of a REXX version: added/changed wording in the REXX section header, formatted the REXX program logic to be able to be viewed on a single screen.)
Line 1:
==a REXX version of a REXX version==
This REXX version is a re-write of version 2 of the REXX entry,   with:
::*   added the required comment so that this REXX version would execute on VM/CMS and MVS/TSO
::*   aligned indentation for all   '''do-end'''   blocks   (and encapsulated statements)
::*   elided distracting superfluous zeroes and decimal pointszeros in (decimal) integers
::*   elides theelided superfluous and detracting use ofdecimal concatenationpoints  in ('''││''')integers
::*   elides the superfluous and distracting 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 capitalizations
::*   maintains the same line for the   '''then'''   clause and the   '''if'''   clause   (no split statements)
::*   uses indentations for all REXX statements in the function
Line 15 ⟶ 16:
::*   REXX variables to hold long literals that would otherwise cause excessive wide REXX statements
::* &nbsp; a comma &nbsp; (instead of a slash) &nbsp; to separate the &nbsp; <big> '''x &nbsp; y''' </big> &nbsp; coördinates of the data points.
::* &nbsp; elides superfluous &nbsp; '''do-end''' &nbsp; block structures; &nbsp; less clutter, easier to read
::* &nbsp; a lotadded more whitespace within some REXX statements and the REXX program's output
::* &nbsp; tests all data possibilities &nbsp; (for showing all the tested non-intersecting conditions)
::* &nbsp; for viewing the calculation in its entirity, all program logic was kept within single viewable screen
 
{{trans|REXX (version 2)}}
<lang rexx>/*REXX program finds (possibly) the intersection of two lines (with diagnostic errors).*/
Line 28 ⟶ 31:
say iSect( 0 0 3 3 0 1 6 7 )
say iSect( 0 0 3 3 8 8 8 8 )
exit /* ═══a═══ ═══b═══ ═══c═══ ═══d══════c═══ stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
iSect: procedure; parse arg xa ya xb yb xc yc xd yd /*optain args ydfrom invocation. */
$=. d2=yc - k2 * xc /*calc.the intersection withor Yerror axismsg.*/
if xa=xb then do; k1=. /*the slope is infinite. end */
else do x1=xa /*CDthe isnX't as vertical line.intersection is XA */
if ycya=ydyb then $= 'points CA and DB are identical'
end else if k2=. then do /* [↑] AB is a /*vertical CDline. */
else do; k2k1=(ydyb-ycya) / (xdxb-xcxa) /*compute the slope of CDAB */
d1=ya - k1 * xa /*calc. intersection with endY axis*/
end /* [↑] AB isn't a end /*normalvertical line*/
if $xc=.xd then do; k2=. /*the slope is infinite. /*no special case so far ··· */
x2=xc /*the C's intersection is y=k1XC * x + d1/
if yc=yd then $= 'points C and D are else doidentical'
end /* [↑] CD elseis a $=@paralvertical /*parallelline. */
else do; k2=(yd-yc) / (xd-xc) /*compute the slope of CD */
d2=yc - y=k2 * x + d2xc /*Ycalc. fromintersection CDwith Y axis*/
end /* [↑] CD isn't a vertical x=(d2-d1) line*/ (k1-k2)
@ident= 'lines AB and CD are identical' /*literal to help shorten a line*/
@paral= 'lines AB and CD are parallel' /* " " " " " " */
$=. /*the intersection[↓] no special orcase errorso msg.far···*/
if xa$=xb. then do if k1=. then if k2=. then if x1=x2 then $=@ident /*is AB a vertical line? identical. */
k1=. /*the slope is infinite. else $=@paral /*parallel. */
x1=xa else do; x=x1 /*the X's intersection is XA /*use X1 */
if ya=yb then $ y=k2 * x + d2 'points A and B/*Y from CD are identical'*/
end
else do else if k2=. then do; x=x2 /*AB isn't a vertical line./*X from CD */
k1=(yb-ya) / (xb-xa) / y=k1 *compute thex slope+ ofd1 AB /*Y from AB */
d1=ya - k1 * xa /*calc. intersection with Y axis*/end
end else if k1=k2 then if d1=d2 then $= @ident
else $= @paral
 
if xc=xd then do /*is CD a vertical line? * else do; x=(d2-d1) / (k1-k2)
k2=. /*the slope is infinite. y=k1 */ x + d1
x2=xc /*the C's intersection is XC end /* [↑] normal*/
if yc=yd then $= 'points C and D are identical'
end
else do /*CD isn't a vertical line. */
k2=(yd-yc) / (xd-xc) /*compute the slope of CD */
d2=yc - k2 * xc /*calc. intersection with Y axis*/
end
 
if $=. then do /*no special case so far ··· */
if k1=. then if k2=. then if x1=x2 then $=@ident /*identical. */
else $=@paral /*parallel. */
else do
x=x1 /*use X1 */
y=k2 * x + d2 /*Y from CD */
end
else if k2=. then do /*vertical CD*/
x=x2 /*X from CD */
y=k1 * x + d1 /*Y from AB */
end
else if k1=k2 then if d1=d2 then $= @ident
else $= @paral
else do /*normal.*/
x=(d2-d1) / (k1-k2)
y=k1 * x + d1
end /*normal line*/
end
 
if $=. then $= 'intersection is at (' || x","y')' /*$ ¬defined?*/
info@ = left( 'a=('xa","ya')', 12) left( 'b=('xb","yb')', 12), /*whitespace.*/
left( 'c=('xc","yc')', 12) left( 'd=('xd","yd')', 12) /* " */
return left(info@, max(51, length(info@) ) ) ' ───► ' $ /*return str.*/</lang>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
Cookies help us deliver our services. By using our services, you agree to our use of cookies.