Ray-casting algorithm: Difference between revisions

m
whitespace
(Updated D code)
m (whitespace)
Line 73:
 
=={{header|Ada}}==
 
polygons.ads:
<lang Ada>package Polygons is
Line 313 ⟶ 312:
=={{header|AutoHotkey}}==
{{incomplete|AutoHotkey}}
<lang ahk>PiP(P,N) ; P is point, N is number of sides
<lang ahk>
PiP(P,N) ; P is point, N is number of sides
{
count := 0
Line 324 ⟶ 322:
else
return true ; P is in the polygon
}</lang ahk>
}
</lang>
 
=={{header|C}}==
Line 576 ⟶ 573:
 
=={{header|CoffeeScript}}==
 
Takes a polygon as a list of points joining segments, and creates segments between them.
<lang coffeescript> Point = (@x,@y) ->
 
<lang coffeescript>
Point = (@x,@y) ->
 
pointInPoly = (point,poly) ->
Line 607 ⟶ 601:
mAB = (b.y - a.y) / (b.x - a.x)
mAP = (p.y - a.y) / (p.x - a.x)
mAP > mAB</lang>
</lang>
 
=={{header|Common Lisp}}==
 
Points are represented as cons cells whose car is an x value and whose cdr is a y value. A line segment is a cons cell of two points. A polygon is a list of line segments.
 
<lang lisp>(defun point-in-polygon (point polygon)
(do ((in-p nil)) ((endp polygon) in-p)
Line 646 ⟶ 637:
 
Testing code
 
<lang lisp>(defparameter *points*
#((0 . 0) (10 . 0) (10 . 10) (0 . 10)
Line 682 ⟶ 672:
test-point
(point-in-polygon test-point polygon)))))</lang>
 
 
=={{header|Fortran}}==
{{works with|Fortran|95 and later}}
 
The following code uses the <tt>Points_Module</tt> defined [[Closest pair problem#Fortran|here]].
 
This module ''defines'' "polygons".
 
<lang fortran>module Polygons
use Points_Module
Line 728 ⟶ 715:
 
The ray casting algorithm module:
 
<lang fortran>module Ray_Casting_Algo
use Polygons
Line 808 ⟶ 794:
 
'''Testing'''
 
<lang fortran>program Pointpoly
use Points_Module
Line 849 ⟶ 834:
 
end program Pointpoly</lang>
 
=={{header|Go}}==
<lang go>package main
Line 1,160 ⟶ 1,146:
 
=={{header|Liberty BASIC}}==
'Translated from C code at: http://alienryderflex.com/polygon/
 
<br>
Displays interactively on-screen.
<lang lb>NoMainWin
NoMainWin
Global sw, sh, verts
 
Line 1,239 ⟶ 1,224:
Function rand(loNum,hiNum)
rand = Int(Rnd(0)*(hiNum-loNum+1)+loNum)
End Function </lang>
</lang>
 
=={{header|OCaml}}==
{{Trans|C}}
 
<lang ocaml>type point = { x:float; y:float }
Line 1,384 ⟶ 1,367:
 
Testing:
 
<lang perl># the following are utilities to use the same Fortran data...
sub point
Line 1,544 ⟶ 1,526:
: (inside (10.0 . 10.0) Exagon)
-> NIL</pre>
 
=={{header|PureBasic}}==
The code below is includes a GUI for drawing a polygon with the mouse that constantly tests whether the mouse is inside or outside the polygon. It displays a message and changes the windows color slightly to indicate if the pointer is inside or outside the polygon being drawn. The routine that does the checking is called inpoly() and it returns a value of one if the point is with the polygon and zero if it isn't.
Line 1,631 ⟶ 1,614:
 
=={{header|Python}}==
<div style="height:50em;overflow:scroll"><lang python>from collections import namedtuple
from pprint import pprint as pp
import sys
Line 1,732 ⟶ 1,715:
for p in testpoints[3:6])
print ' ', '\t'.join("%s: %s" % (p, ispointinside(p, poly))
for p in testpoints[6:])</lang></div>
 
'''Sample output'''
Line 1,788 ⟶ 1,771:
 
'''Helper routine to convert Fortran Polygons and points to Python'''
<div style="height:20em;overflow:scroll">
<lang python>def _convert_fortran_shapes():
point = Pt
Line 1,815 ⟶ 1,797:
print ' ', ',\n '.join(str(e) for e in p.edges) + '\n )),'
print ' ]'
_convert_fortran_shapes()</lang></div>
 
=={{header|R}}==
Line 1,906 ⟶ 1,888:
=={{header|Smalltalk}}==
{{works with|GNU Smalltalk}}
 
The class Segment holds the code to test if a ray starting from a point (and going towards "right") intersects the segment (method <tt>doesIntersectRayFrom</tt>); while the class Polygon hosts the code to test if a point is inside the polygon (method <tt>pointInside</tt>).
 
<lang smalltalk>Object subclass: Segment [
|pts|
Line 1,988 ⟶ 1,968:
 
'''Testing'''
 
<lang smalltalk>|points names polys|
 
Anonymous user