Another update from ingydotnet^djgoku
This commit is contained in:
parent
91df62d461
commit
948b86eafa
7604 changed files with 108452 additions and 22726 deletions
|
|
@ -20,7 +20,7 @@ program Pointpoly
|
|||
polys(3) = create_polygon(pts, (/ 1,5, 5,4, 4,8, 8,7, 7,3, 3,2, 2,5 /) )
|
||||
polys(4) = create_polygon(pts, (/ 11,12, 12,10, 10,13, 13,14, 14,9, 9,11 /) )
|
||||
|
||||
names = (/ "square", "square hole", "strange", "exagon" /)
|
||||
names = (/ "square", "square hole", "strange", "hexagon" /)
|
||||
|
||||
p = (/ point(5,5), point(5, 8), point(-10, 5), point(0,5), point(10,5), &
|
||||
point(8,5), point(10,10) /)
|
||||
|
|
|
|||
|
|
@ -10,7 +10,7 @@ sub create_polygon
|
|||
for(my $i = 0; $i < $#$sides; $i += 2) {
|
||||
push @poly, [ $pts->[$sides->[$i]-1], $pts->[$sides->[$i+1]-1] ];
|
||||
}
|
||||
@poly;
|
||||
\@poly;
|
||||
}
|
||||
|
||||
my @pts = ( point(0,0), point(10,0), point(10,10), point(0,10),
|
||||
|
|
@ -18,20 +18,22 @@ my @pts = ( point(0,0), point(10,0), point(10,10), point(0,10),
|
|||
point(0,5), point(10,5),
|
||||
point(3,0), point(7,0), point(7,10), point(3,10) );
|
||||
|
||||
my @squared = create_polygon(\@pts, [ 1,2, 2,3, 3,4, 4,1 ] );
|
||||
my @squaredhole = create_polygon(\@pts, [ 1,2, 2,3, 3,4, 4,1, 5,6, 6,7, 7,8, 8,5 ] );
|
||||
my @strange = create_polygon(\@pts, [ 1,5, 5,4, 4,8, 8,7, 7,3, 3,2, 2,5 ] );
|
||||
my @exagon = create_polygon(\@pts, [ 11,12, 12,10, 10,13, 13,14, 14,9, 9,11 ]) ;
|
||||
my %pgs = (
|
||||
squared => create_polygon(\@pts, [ 1,2, 2,3, 3,4, 4,1 ] ),
|
||||
squaredhole => create_polygon(\@pts, [ 1,2, 2,3, 3,4, 4,1, 5,6, 6,7, 7,8, 8,5 ] ),
|
||||
strange => create_polygon(\@pts, [ 1,5, 5,4, 4,8, 8,7, 7,3, 3,2, 2,5 ] ),
|
||||
exagon => create_polygon(\@pts, [ 11,12, 12,10, 10,13, 13,14, 14,9, 9,11 ]) ,
|
||||
);
|
||||
|
||||
my @p = ( point(5,5), point(5, 8), point(-10, 5), point(0,5), point(10,5), &
|
||||
point(8,5), point(10,10) );
|
||||
|
||||
foreach my $pol ( qw(squared squaredhole strange exagon) ) {
|
||||
foreach my $pol ( sort keys %pgs ) {
|
||||
no strict 'refs';
|
||||
print "$pol\n";
|
||||
my @rp = @{$pol};
|
||||
my $rp = $pgs{$pol};
|
||||
foreach my $tp ( @p ) {
|
||||
print "\t($tp->[0],$tp->[1]) " .
|
||||
( point_in_polygon($tp, \@rp) ? "INSIDE" : "OUTSIDE" ) . "\n";
|
||||
( point_in_polygon($tp, $rp) ? "INSIDE" : "OUTSIDE" ) . "\n";
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -47,8 +47,8 @@ def ispointinside(p, poly):
|
|||
for edge in poly.edges ))
|
||||
|
||||
def polypp(poly):
|
||||
print "\n Polygon(name='%s', edges=(" % poly.name
|
||||
print ' ', ',\n '.join(str(e) for e in poly.edges) + '\n ))'
|
||||
print ("\n Polygon(name='%s', edges=(" % poly.name)
|
||||
print (' ', ',\n '.join(str(e) for e in poly.edges) + '\n ))')
|
||||
|
||||
if __name__ == '__main__':
|
||||
polys = [
|
||||
|
|
@ -91,12 +91,12 @@ if __name__ == '__main__':
|
|||
Pt(x=10, y=5), Pt(x=8, y=5),
|
||||
Pt(x=10, y=10))
|
||||
|
||||
print "\n TESTING WHETHER POINTS ARE WITHIN POLYGONS"
|
||||
print ("\n TESTING WHETHER POINTS ARE WITHIN POLYGONS")
|
||||
for poly in polys:
|
||||
polypp(poly)
|
||||
print ' ', '\t'.join("%s: %s" % (p, ispointinside(p, poly))
|
||||
for p in testpoints[:3])
|
||||
print ' ', '\t'.join("%s: %s" % (p, ispointinside(p, poly))
|
||||
for p in testpoints[3:6])
|
||||
print ' ', '\t'.join("%s: %s" % (p, ispointinside(p, poly))
|
||||
for p in testpoints[6:])
|
||||
print (' ', '\t'.join("%s: %s" % (p, ispointinside(p, poly))
|
||||
for p in testpoints[:3]))
|
||||
print (' ', '\t'.join("%s: %s" % (p, ispointinside(p, poly))
|
||||
for p in testpoints[3:6]))
|
||||
print (' ', '\t'.join("%s: %s" % (p, ispointinside(p, poly))
|
||||
for p in testpoints[6:]))
|
||||
|
|
|
|||
|
|
@ -1,49 +1,52 @@
|
|||
/*REXX program to see if a horizontal ray from pt P intersects a polygon*/
|
||||
call points 5 5, 5 8, -10 5, 0 5, 10 5, 8 5, 10 10
|
||||
call polygon 0 0, 10 0, 10 10, 0 10 ; call test 'square'
|
||||
call polygon 0 0, 10 0, 10 10, 0 10, 2.5 2.5, 7.5 2.5, 7.5 7.5, 2.5 7.5 ; call test 'square hole'
|
||||
call polygon 0 0, 2.5 2.5, 0 10, 2.5 7.5, 7.5 7.5, 10 10, 10 0 ; call test 'irregular'
|
||||
call polygon 3 0, 7 0, 10 5, 7 10, 3 10, 0 5 ; call test 'exagon'
|
||||
exit /*stick a fork in it, we're done.*/
|
||||
/*──────────────────────────────────IN_OUT subroutine────────────────────*/
|
||||
in_out: procedure expose point. poly. /*note: // is division remainder.*/
|
||||
parse arg p; #=0; do side=1 to poly.0 by 2; #=#+ray_intersect(p,side)
|
||||
/*REXX pgm checks to see if a horizontal ray from point P intersects a polygon*/
|
||||
call points 5 5, 5 8, -10 5, 0 5, 10 5, 8 5, 10 10
|
||||
A=2.5; B=7.5 /*◄───for shorter args*/
|
||||
call poly 0 0, 10 0, 10 10, 0 10 ; call test 'square'
|
||||
call poly 0 0, 10 0, 10 10, 0 10, A A, B A, B B, A B ; call test 'square hole
|
||||
call poly 0 0, A A, 0 10, A B, B B, 10 10, 10 0 ; call test 'irregular'
|
||||
call poly 3 0, 7 0, 10 5, 7 10, 3 10, 0 5 ; call test 'hexagon'
|
||||
exit /*stick a fork in it, we're all done. */
|
||||
/*────────────────────────────────────────────────────────────────────────────*/
|
||||
in_out: procedure expose point. poly.; parse arg p; #=0
|
||||
do side=1 to poly.0 by 2; #=#+ray_intersect(p,side)
|
||||
end /*side*/
|
||||
return #//2 /*odd=inside, return 1; else 0.*/
|
||||
/*──────────────────────────────────POINTS subroutine───────────────────*/
|
||||
points: n=0; v='POINT.'; do j=1 for arg(); n=n+1
|
||||
call value v||n'.X', word(arg(j),1)
|
||||
call value v||n'.Y', word(arg(j),2)
|
||||
end /*j*/
|
||||
call value v'0',n /*define number of points.*/
|
||||
return
|
||||
/*──────────────────────────────────POLYGON subroutine──────────────────*/
|
||||
polygon: n=0; v='POLY.'; parse arg Fx Fy
|
||||
return #//2 /*ODD is inside. EVEN is outside. */
|
||||
/*────────────────────────────────────────────────────────────────────────────*/
|
||||
points: n=0; v='POINT.'; do j=1 for arg(); n=n+1; _=arg(j); parse var _ xx yy
|
||||
call value v||n'.X',xx
|
||||
call value v||n'.Y',yy
|
||||
end /*j*/
|
||||
call value v'0',n /*define the number of points.*/
|
||||
return
|
||||
/*────────────────────────────────────────────────────────────────────────────*/
|
||||
poly: n=0; v='POLY.'; parse arg Fx Fy /* [↓] process the X,Y points*/
|
||||
|
||||
do j=1 for arg(); _=arg(j); n=n+1
|
||||
call value v||n'.X', word(_,1); call value v||n'.Y', word(_,2)
|
||||
if n//2 then iterate
|
||||
n=n+1
|
||||
call value v||n'.X', word(_,1); call value v||n'.Y', word(_,2)
|
||||
end /*j*/
|
||||
n=n+1
|
||||
call value v||n".X", Fx; call value v||n".Y", Fy; call value v'0',n
|
||||
return /*POLY.0 is # of segments/sides.*/
|
||||
/*──────────────────────────────────RAY_INTERSECT subroutine────────────*/
|
||||
ray_intersect: procedure expose point. poly.; parse arg ?,s; sp=s+1
|
||||
epsilon = '1e'||(digits()%2); infinity = '1e'||(digits()*2)
|
||||
Px=point.?.x; Py=point.?.y
|
||||
Ax=poly.s.x; Bx=poly.sp.x ; Ay=poly.s.y; By=poly.sp.y
|
||||
if Ay>By then parse value Ax Ay Bx By with Bx By Ax Ay
|
||||
if Py=Ay | Py=By then Py=Py+epsilon
|
||||
if Py<Ay | Py>By | Px>max(Ax,Bx) then return 0
|
||||
if Px<min(Ax,Bx) then return 1
|
||||
if Ax\=Bx then m_red = (By-Ay) / (Bx-Ax); else m_red = infinity
|
||||
if Ax\=Px then m_blue = (Py-Ay) / (Px-Ax); else return 1
|
||||
return m_blue>=m_red
|
||||
/*──────────────────────────────────TEST procedure──────────────────────*/
|
||||
test: say; do k=1 for point.0 /*traipse through each test point*/
|
||||
say ' ['arg(1)"] point:" right(point.k.x','point.k.y, 9),
|
||||
" is " word('outside inside', in_out(k)+1)
|
||||
end /*k*/
|
||||
return
|
||||
do j=1 for arg(); n=n+1; _=arg(j); parse var _ xx yy
|
||||
call value v||n'.X', word(_,1); call value v||n'.Y', word(_,2)
|
||||
if n//2 then iterate
|
||||
n=n+1
|
||||
call value v||n'.X', word(_,1); call value v||n'.Y', word(_,2)
|
||||
end /*j*/
|
||||
n=n+1
|
||||
call value v||n".X", Fx; call value v||n".Y", Fy; call value v'0',n
|
||||
return /*POLY.0 is number of segments/sides.*/
|
||||
/*────────────────────────────────────────────────────────────────────────────*/
|
||||
ray_intersect: procedure expose point. poly.; parse arg ?,s; sp=s+1
|
||||
epsilon='1e' || (digits()%2); infinity='1e' || (digits() *2)
|
||||
Px=point.?.x; Ax=poly.s.x; Ay=poly.s.y
|
||||
Py=point.?.y; Bx=poly.sp.x; By=poly.sp.y /* [↓] do a swap*/
|
||||
if Ay>By then parse value Ax Ay Bx By with Bx By Ax Ay
|
||||
if Py=Ay | Py=By then Py=Py+epsilon
|
||||
if Py<Ay | Py>By | Px>max(Ax,Bx) then return 0
|
||||
if Px<min(Ax,Bx) then return 1
|
||||
if Ax\=Bx then m_red =(By-Ay)/(Bx-Ax)
|
||||
else m_red =infinity
|
||||
if Ax\=Px then m_blue=(Py-Ay)/(Px-Ax)
|
||||
else return 1
|
||||
return m_blue>=m_red
|
||||
/*────────────────────────────────────────────────────────────────────────────*/
|
||||
test: say; do k=1 for point.0; say right(' ['arg(1)"] point:",30),
|
||||
right(point.k.x', 'point.k.y, 9) " is ",
|
||||
word('outside inside', in_out(k)+1)
|
||||
end /*k*/
|
||||
return
|
||||
|
|
|
|||
46
Task/Ray-casting-algorithm/Scala/ray-casting-algorithm.scala
Normal file
46
Task/Ray-casting-algorithm/Scala/ray-casting-algorithm.scala
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
case class Figure(name: String, edges: ((Double, Double), (Double, Double))*) {}
|
||||
|
||||
object Ray_casting extends App {
|
||||
import Math._
|
||||
import Double._
|
||||
|
||||
val figures = Array(Figure("Square", ((0.0, 0.0), (10.0, 0.0)),
|
||||
((10.0, 0.0), (10.0, 10.0)), ((10.0, 10.0), (0.0, 10.0)),
|
||||
((0.0, 10.0), (0.0, 0.0))),
|
||||
Figure("Square hole", ((0.0, 0.0), (10.0, 0.0)), ((10.0, 0.0), (10.0, 10.0)),
|
||||
((10.0, 10.0), (0.0, 10.0)), ((0.0, 10.0), (0.0, 0.0)),
|
||||
((2.5, 2.5), (7.5, 2.5)), ((7.5, 2.5), (7.5, 7.5)),
|
||||
((7.5, 7.5), (2.5, 7.5)), ((2.5, 7.5), (2.5, 2.5))),
|
||||
Figure("Strange", ((0.0, 0.0), (2.5, 2.5)), ((2.5, 2.5), (0.0, 10.0)),
|
||||
((0.0, 10.0), (2.5, 7.5)), ((2.5, 7.5), (7.5, 7.5)),
|
||||
((7.5, 7.5), (10.0, 10.0)), ((10.0, 10.0), (10.0, 0.0)),
|
||||
((10.0, 0), (2.5, 2.5))),
|
||||
Figure("Exagon", ((3.0, 0.0), (7.0, 0.0)), ((7.0, 0.0), (10.0, 5.0)),
|
||||
((10.0, 5.0), (7.0, 10.0)), ((7.0, 10.0), (3.0, 10.0)),
|
||||
((3.0, 10.0), (0.0, 5.0)), ((0.0, 5.0), (3.0, 0.0))))
|
||||
|
||||
val points = Array((5.0, 5.0), (5.0, 8.0), (-10.0, 5.0), (0.0, 5.0), (10.0, 5.0), (8.0, 5.0), (10.0, 10.0))
|
||||
|
||||
figures foreach { f =>
|
||||
println("Is point inside figure " + f.name + '?')
|
||||
points foreach { p => println(" " + p + ": " + contains(f, p)) }
|
||||
println
|
||||
}
|
||||
|
||||
private def raySegI(p: (Double, Double), e: ((Double, Double), (Double, Double))): Boolean = {
|
||||
val epsilon = 0.00001
|
||||
if (e._1._2 > e._2._2)
|
||||
return raySegI(p, (e._2, e._1))
|
||||
if (p._2 == e._1._2 || p._2 == e._2._2)
|
||||
return raySegI((p._1, p._2 + epsilon), e)
|
||||
if (p._2 > e._2._2 || p._2 < e._1._2 || p._1 > max(e._1._1, e._2._1))
|
||||
return false
|
||||
if (p._1 < min(e._1._1, e._2._1))
|
||||
return true
|
||||
val blue = if (abs(e._1._1 - p._1) > MinValue) (p._2 - e._1._2) / (p._1 - e._1._1) else MaxValue
|
||||
val red = if (abs(e._1._1 - e._2._1) > MinValue) (e._2._2 - e._1._2) / (e._2._1 - e._1._1) else MaxValue
|
||||
blue >= red
|
||||
}
|
||||
|
||||
private def contains(f: Figure, p: (Double, Double)) = f.edges.count(raySegI(p, _)) % 2 != 0
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue