Talk:Find the intersection of two lines: Difference between revisions
Talk:Find the intersection of two lines (view source)
Revision as of 21:04, 29 December 2021
, 2 years ago→javascript
m (→a REXX version of a REXX version: included comments that were added to REXX version 2, added more comments.) |
|||
(6 intermediate revisions by 4 users not shown) | |||
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
::*
::* 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
::* maintains the same line for the '''then''' clause and the '''if''' clause (no split statements)
::* uses 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 <big> '''x y''' </big> coördinates of the data points.
::* elides superfluous '''do-end''' block structures; less clutter, easier to read
::*
::* tests all data possibilities (for showing all the tested non-intersecting conditions)
::* 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 29 ⟶ 33:
exit /* ═══a═══ ═══b═══ ═══c═══ ═══c═══ stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
iSect: procedure;
else do; k2=(yd-yc) / (xd-xc) /*compute the slope of CD */
@ident= 'lines AB and CD are identical' /*literal to help shorten a line*/
@paral= 'lines AB and CD are parallel' /* " " " " " " */
if
end
else $= @paral▼
▲ 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*/
▲ if $=. then do /*no special case so far ··· */
▲ else $=@paral /*parallel. */
▲ else do
▲ y=k2 * x + d2 /*Y from CD */
▲ end
▲ else if k2=. then do /*vertical CD*/
▲ end
▲ else $= @paral
▲ x=(d2-d1) / (k1-k2)
▲ y=k1 * x + d1
▲ end /*normal line*/
end▼
if $=. then $= 'intersection is at (' || x","y')' /*$ ¬defined?*/
return left(
{{out|output|text= when using the default inputs:}}
<pre>
Line 105 ⟶ 98:
:::: But let's agree that our taste as far as formatting is concerned is vastly different.
:::: And I added commentary to my version. --[[User:Walterpachl|Walterpachl]] ([[User talk:Walterpachl|talk]]) 09:29, 19 May 2017 (UTC)
== Clojure version does not handle edge case case of undefined slope (vertical line) ==
The part that calculates m (slope) fails if `(- x2 x1)` is zero.
(defn compute-line [pt1 pt2]
(let [[x1 y1] pt1
{:slope m
:offset (- y1 (* m x1))}))
Should the clojure version do something other than throw an exception if the compute-line fn receives a vertical line like [0 0] [0 6], but also all cases like [X Y] [X Z], I would suspect.
Currently it just throws on divide by zero, which means you can not find the intersection if the both x-coords points of one line are the same.
== javascript ==
can we have a webpage version of the code?
:I just added an online link for a transpiled version of Phix (am currently doing that sort of thing to lots of pages), not exactly what you asked for I know, but you could use that as a starting point: rip out my p2js.js, make X=0 and Y=1, replace $subse() with plain e[X] etc, kill off all those ugly "sequence", a few "[," ==> "[", and lastly you'll need a replacement for my print(). HTH. --[[User:Petelomax|Pete Lomax]] ([[User talk:Petelomax|talk]]) 21:04, 29 December 2021 (UTC) PS Please in future sign your posts using <nowiki>--~~~~</nowiki> (without the nowiki bits).
|