Talk:Find the intersection of two lines

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)
Translation of: REXX (version 2)

<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
==================================================================================
If I were to translate many of the Rexx programs of GS to my liking I'd be busy for months.
  •  Here is the Rexx program as I wrote it in my style:::*  

http://rosettacode.org/wiki/Find_the_intersection_of_two_lines#version_2

  •  Any opinions from any peers? --Walterpachl (talk) 06:01, 19 May 2017 (UTC)

There is no reason to take it personally.   It wasn't meant as a criticism, it is just another version in a different style, albeit a fair number of (style) differences.   Everybody's code can be improved (as least, the style can be changed).   In this case, I elided a few superfluous statements, which, in my opinion, didn't add anything to the REXX program or make it easier to understand/peruse.   I didn't appreciate your style of capitalization, misaligned DO-END statements (and the intervening/encapsulating REXX statements), split IF-THEN clauses, and much more.     But, that's only my opinion and preferences,   I merely added a version that I found easier to read and understand (and I hoped others will appreciate this version), and I also removed superfluous DO-END blocks and such.   Note that this re-written REXX version was added in the   talk   section, not on the   page   section so as to not clutter up the main page.   I found that that particular REXX version was so difficult to follow and understand (the IF logic) with all the multiple misaligned DO-END and compound IF THEN-ELSE statements.   The version (above) that I re-wrote speaks to style and understandability.   There are many styles to write REXX programs in, and this is just one of them.   Nobody's style is everybody's cup of tea.   -- Gerard Schildberger (talk) 08:22, 19 May 2017 (UTC)

Return to "Find the intersection of two lines" page.