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

From Rosetta Code
Content added Content deleted
m (well, waiting for other opinions)
m (→‎a REXX version of a REXX version: included comments that were added to REXX version 2, added more comments.)
Line 19: Line 19:
{{trans|REXX (version 2)}}
{{trans|REXX (version 2)}}
<lang rexx>/*REXX program finds (possibly) the intersection of two lines (with diagnostic errors).*/
<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( 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 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 1 1 0 1 7 )
say iSect( 0 0 0 0 0 3 10 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 0 6 6 )
say iSect( 0 0 3 3 0 1 6 7 )
say iSect( 0 0 3 3 0 1 6 7 )
say iSect( 0 0 3 3 8 8 8 8 )
say iSect( 0 0 3 3 8 8 8 8 )
exit /*stick a fork in it, we're all done. */
exit /* ═══a═══ ═══b═══ ═══c═══ ═══c═══ stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
iSect: procedure; parse arg xa ya xb yb xc yc xd yd
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*/
@ident= 'lines AB and CD are identical' /*literal to help shorten a line*/
@paral= 'lines AB and CD are parallel' /* " " " " " " */
@paral= 'lines AB and CD are parallel' /* " " " " " " */
$=. /*the interection (or error msg)*/
$=. /*the intersection or error msg.*/
if xa=xb then do
if xa=xb then do /*is AB a vertical line? */
k1=.
k1=. /*the slope is infinite. */
x1=xa
x1=xa /*the X's intersection is XA */
if ya=yb then $= 'points A and B are identical'
if ya=yb then $= 'points A and B are identical'
end
end
else do
else do /*AB isn't a vertical line. */
k1=(yb-ya) / (xb-xa)
k1=(yb-ya) / (xb-xa) /*compute the slope of AB */
d1=ya - k1 * xa
d1=ya - k1 * xa /*calc. intersection with Y axis*/
end
end


if xc=xd then do
if xc=xd then do /*is CD a vertical line? */
k2=.
k2=. /*the slope is infinite. */
x2=xc
x2=xc /*the C's intersection is XC */
if yc=yd then $= 'points C and D are identical'
if yc=yd then $= 'points C and D are identical'
end
end
else do
else do /*CD isn't a vertical line. */
k2=(yd-yc) / (xd-xc)
k2=(yd-yc) / (xd-xc) /*compute the slope of CD */
d2=yc - k2 * xc
d2=yc - k2 * xc /*calc. intersection with Y axis*/
end
end


if $=. then do
if $=. then do /*no special case so far ··· */
if k1=. then if k2=. then if x1=x2 then $=@ident
if k1=. then if k2=. then if x1=x2 then $=@ident /*identical. */
else $=@paral
else $=@paral /*parallel. */
else do
else do
x=x1
x=x1 /*use X1 */
y=k2 * x + d2
y=k2 * x + d2 /*Y from CD */
end
end
else if k2=. then do
else if k2=. then do /*vertical CD*/
x=x2
x=x2 /*X from CD */
y=k1 * x + d1
y=k1 * x + d1 /*Y from AB */
end
end
else if k1=k2 then if d1=d2 then $= @ident
else if k1=k2 then if d1=d2 then $= @ident
else $= @paral
else $= @paral
else do
else do /*normal.*/
x=(d2-d1) / (k1-k2)
x=(d2-d1) / (k1-k2)
y=k1 * x + d1
y=k1 * x + d1
end
end /*normal line*/
end
end


if $=. then $= 'intersection is at (' || x","y')'
if $=. then $= 'intersection is at (' || x","y')' /*$ ¬defined?*/
info= 'a=('xa","ya') b=('xb","yb') c=('xc","yc') d=('xd","yd')'
info=left( 'a=('xa","ya')', 12) left( 'b=('xb","yb')', 12),
return left(info, max(50, length(info) ) ) ' ───► ' $</lang>
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 input:}}
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
<pre>
a=(4,0) b=(6,10) c=(0,3) d=(10,7) ───► intersection is at (5,5)
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,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,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=(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,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=(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
a=(0,0) b=(3,3) c=(8,8) d=(8,8) ───► points C and D are identical
</pre>
</pre>

==============================================================================================
==============================================================================================
: If I were to translate many of the Rexx programs of GS to my liking I'd be busy for months.
: If I were to translate many of the Rexx programs of GS to my liking I'd be busy for months.
Line 94: Line 96:
-----
-----


::: There is no reason to take it personally. &nbsp; It wasn't meant as a criticism, it is just another version in a different style, albeit a fair number of (style) differences. &nbsp; <u>Everybody's</u> code can be improved (as least, the style can be changed). &nbsp; 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. &nbsp; 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. &nbsp; &nbsp; But, that's only my opinion and preferences, &nbsp; 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. &nbsp; Note that this re-written REXX version was added in the &nbsp; ''talk'' &nbsp; section, not on the &nbsp; ''page'' &nbsp; section so as to not clutter up the main page. &nbsp; 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. &nbsp; The version (above) that I re-wrote speaks to style and understandability. &nbsp; There are many styles to write REXX programs in, and this is just one of them. &nbsp; Nobody's style is everybody's cup of tea. &nbsp; -- [[User:Gerard Schildberger|Gerard Schildberger]] ([[User talk:Gerard Schildberger|talk]]) 08:22, 19 May 2017 (UTC)
::: There is no reason to take it personally. &nbsp; It wasn't meant as a criticism, it is just another version in a different style, albeit a fair number of (style) differences. &nbsp; <u>Everybody's</u> code can be improved (as least, the style can be changed). &nbsp; 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. &nbsp; 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. &nbsp; &nbsp; But, that's only my opinion and preferences, &nbsp; 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. &nbsp; Note that this re-written REXX version was added in the &nbsp; ''discussion'' &nbsp; section, not on the &nbsp; ''page'' &nbsp; section so as to not clutter up the main page. &nbsp; 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. &nbsp; The version (above) that I re-wrote speaks to style and understandability. &nbsp; There are many styles to write REXX programs in, and this is just one of them. &nbsp; Nobody's style is everybody's cup of tea. &nbsp; -- [[User:Gerard Schildberger|Gerard Schildberger]] ([[User talk:Gerard Schildberger|talk]]) 08:22, 19 May 2017 (UTC)


-----
-----

Revision as of 23:08, 16 August 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)
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 /* ═══a═══ ═══b═══ ═══c═══ ═══c═══ 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 intersection or error msg.*/
      if xa=xb  then do                                /*is  AB  a vertical line?      */
                     k1=.                              /*the slope is infinite.        */
                     x1=xa                             /*the  X's  intersection is  XA */
                     if ya=yb  then $= 'points  A  and  B   are identical'
                     end
                else do                                /*AB  isn't a vertical line.    */
                     k1=(yb-ya) / (xb-xa)              /*compute the slope of  AB      */
                     d1=ya - k1 * xa                   /*calc. intersection with Y axis*/
                     end
      if xc=xd  then do                                /*is  CD  a vertical line?      */
                     k2=.                              /*the slope is infinite.        */
                     x2=xc                             /*the  C's  intersection is  XC */
                     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),
           left( 'c=('xc","yc')', 12)     left( 'd=('xd","yd')', 12)
      return left(info, max(51, length(info) ) )          ' ───► '     $  /*return str.*/</lang>
output   when using the default inputs:
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   discussion   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)

The only thing I liked about this variation is the presentation of the result.
The a= etc. should be A= etc. to be consistent.
I dislike that the variation can no longer be used with ooRexx :-(
and i cannot appreciate the landscape formatting!
But let's agree that our taste as far as formatting is concerned is vastly different.
And I added commentary to my version. --Walterpachl (talk) 09:29, 19 May 2017 (UTC)