Add tasks for all the new languages
This commit is contained in:
parent
9dc3c2bb62
commit
bba7bfd280
13208 changed files with 134745 additions and 0 deletions
|
|
@ -0,0 +1,114 @@
|
|||
Type Point
|
||||
As Single x,y
|
||||
End Type
|
||||
|
||||
Function inpolygon(p1() As Point,p2 As Point) As Integer
|
||||
#Macro isleft2(L,p)
|
||||
-Sgn( (L(1).x-L(2).x)*(p.y-L(2).y) - (p.x-L(2).x)*(L(1).y-L(2).y))
|
||||
#EndMacro
|
||||
Dim As Integer index,nextindex
|
||||
Dim k As Integer=UBound(p1)+1
|
||||
Dim send (1 To 2) As Point
|
||||
Dim wn As Integer=0
|
||||
For n As Integer=1 To UBound(p1)
|
||||
index=n Mod k:nextindex=(n+1) Mod k
|
||||
If nextindex=0 Then nextindex=1
|
||||
send(1).x=p1(index).x:send(2).x=p1(nextindex).x
|
||||
send(1).y=p1(index).y:send(2).y=p1(nextindex).y
|
||||
If p1(index).y<=p2.y Then
|
||||
If p1(nextindex).y>p2.y Then
|
||||
If isleft2(send,p2)>=0 Then '=
|
||||
wn=wn+1
|
||||
End If
|
||||
End If
|
||||
Else
|
||||
If p1(nextindex).y<=p2.y Then
|
||||
If isleft2(send,p2)<=0 Then'=
|
||||
wn=wn-1
|
||||
End If
|
||||
End If
|
||||
End If
|
||||
Next n
|
||||
Return wn
|
||||
End Function
|
||||
|
||||
|
||||
Dim As Point square(1 To 4) ={(0,0),(10,0),(10,10),(0,10)}
|
||||
|
||||
Dim As Point hole(1 To 4) ={(2.5,2.5),(7.5,2.5),(7.5,7.5),(2.5,7.5)}
|
||||
|
||||
Dim As Point strange(1 To 8) ={(0,0),(2.5,2.5),(0,10),(2.5,7.5),_
|
||||
(7.5,7.5),(10,10),(10,0),(2.5,2.5)}
|
||||
|
||||
Dim As Point exagon(1 To 6) ={(3,0),(7,0),(10,5),(7,10),(3,10),(0,5)}
|
||||
|
||||
'printouts
|
||||
For z As Integer=1 To 4
|
||||
Select Case z
|
||||
Case 1: Print "squared"
|
||||
Print "(5,5) " ;Tab(12);
|
||||
If inpolygon(square(),Type<Point>(5,5)) Then Print "in" Else Print "out"
|
||||
Print "(5,8) " ;Tab(12);
|
||||
If inpolygon(square(),Type<Point>(5,8)) Then Print "in" Else Print "out"
|
||||
Print "(-10,5) " ;Tab(12);
|
||||
If inpolygon(square(),Type<Point>(-10,5)) Then Print "in" Else Print "out"
|
||||
Print "(0,5) " ;Tab(12);
|
||||
If inpolygon(square(),Type<Point>(0,5)) Then Print "in" Else Print "out"
|
||||
Print "(10,5) " ;Tab(12);
|
||||
If inpolygon(square(),Type<Point>(10,5)) Then Print "in" Else Print "out"
|
||||
Print "(8,5) " ;Tab(12);
|
||||
If inpolygon(square(),Type<Point>(8,5)) Then Print "in" Else Print "out"
|
||||
Print "(10,10) " ;Tab(12);
|
||||
If inpolygon(square(),Type<Point>(10,10)) Then Print "in" Else Print "out"
|
||||
Print
|
||||
Case 2:Print "squared hole"
|
||||
Print "(5,5) " ;Tab(12);
|
||||
If Not inpolygon(hole(),Type<Point>(5,5)) And inpolygon(square(),Type<Point>(5,5)) Then Print "in" Else Print "out"
|
||||
Print "(5,8) " ;Tab(12);
|
||||
If Not inpolygon(hole(),Type<Point>(5,8)) And inpolygon(square(),Type<Point>(5,8))Then Print "in" Else Print "out"
|
||||
Print "(-10,5) " ;Tab(12);
|
||||
If Not inpolygon(hole(),Type<Point>(-10,5))And inpolygon(square(),Type<Point>(-10,5)) Then Print "in" Else Print "out"
|
||||
Print "(0,5) " ;Tab(12);
|
||||
If Not inpolygon(hole(),Type<Point>(0,5))And inpolygon(square(),Type<Point>(0,5)) Then Print "in" Else Print "out"
|
||||
Print "(10,5) " ;Tab(12);
|
||||
If Not inpolygon(hole(),Type<Point>(10,5))And inpolygon(square(),Type<Point>(10,5)) Then Print "in" Else Print "out"
|
||||
Print "(8,5) " ;Tab(12);
|
||||
If Not inpolygon(hole(),Type<Point>(8,5))And inpolygon(square(),Type<Point>(8,5)) Then Print "in" Else Print "out"
|
||||
Print "(10,10) " ;Tab(12);
|
||||
If Not inpolygon(hole(),Type<Point>(10,10))And inpolygon(square(),Type<Point>(10,10)) Then Print "in" Else Print "out"
|
||||
Print
|
||||
Case 3:Print "strange"
|
||||
Print "(5,5) " ;Tab(12);
|
||||
If inpolygon(strange(),Type<Point>(5,5)) Then Print "in" Else Print "out"
|
||||
Print "(5,8) " ;Tab(12);
|
||||
If inpolygon(strange(),Type<Point>(5,8)) Then Print "in" Else Print "out"
|
||||
Print "(-10,5) " ;Tab(12);
|
||||
If inpolygon(strange(),Type<Point>(-10,5)) Then Print "in" Else Print "out"
|
||||
Print "(0,5) " ;Tab(12);
|
||||
If inpolygon(strange(),Type<Point>(0,5)) Then Print "in" Else Print "out"
|
||||
Print "(10,5) " ;Tab(12);
|
||||
If inpolygon(strange(),Type<Point>(10,5)) Then Print "in" Else Print "out"
|
||||
Print "(8,5) " ;Tab(12);
|
||||
If inpolygon(strange(),Type<Point>(8,5)) Then Print "in" Else Print "out"
|
||||
Print "(10,10) " ;Tab(12);
|
||||
If inpolygon(strange(),Type<Point>(10,10)) Then Print "in" Else Print "out"
|
||||
Print
|
||||
Case 4:Print "exagon"
|
||||
Print "(5,5) " ;Tab(12);
|
||||
If inpolygon(exagon(),Type<Point>(5,5)) Then Print "in" Else Print "out"
|
||||
Print "(5,8) " ;Tab(12);
|
||||
If inpolygon(exagon(),Type<Point>(5,8)) Then Print "in" Else Print "out"
|
||||
Print "(-10,5) " ;Tab(12);
|
||||
If inpolygon(exagon(),Type<Point>(-10,5)) Then Print "in" Else Print "out"
|
||||
Print "(0,5) " ;Tab(12);
|
||||
If inpolygon(exagon(),Type<Point>(0,5)) Then Print "in" Else Print "out"
|
||||
Print "(10,5) " ;Tab(12);
|
||||
If inpolygon(exagon(),Type<Point>(10,5)) Then Print "in" Else Print "out"
|
||||
Print "(8,5) " ;Tab(12);
|
||||
If inpolygon(exagon(),Type<Point>(8,5)) Then Print "in" Else Print "out"
|
||||
Print "(10,10) " ;Tab(12);
|
||||
If inpolygon(exagon(),Type<Point>(10,10)) Then Print "in" Else Print "out"
|
||||
Print
|
||||
End Select
|
||||
Next z
|
||||
Sleep
|
||||
103
Task/Ray-casting-algorithm/Phix/ray-casting-algorithm.phix
Normal file
103
Task/Ray-casting-algorithm/Phix/ray-casting-algorithm.phix
Normal file
|
|
@ -0,0 +1,103 @@
|
|||
constant true = (1=1), false = not true
|
||||
constant inf = 1e300*1e300
|
||||
|
||||
function rayIntersectsSegment(sequence point, sequence segment)
|
||||
atom {pX,pY} = point
|
||||
atom {{aX,aY},{bX,bY}} = segment
|
||||
atom m_red,m_blue
|
||||
-- ensure {aX,aY} is the segment end-point with the smallest y coordinate
|
||||
if aY>bY then
|
||||
{{bX,bY},{aX,aY}} = segment
|
||||
end if
|
||||
if pY=aY or pY=bY then
|
||||
--
|
||||
-- Consider a ray passing through the top or left corner of a diamond:
|
||||
-- /
|
||||
-- --- or -
|
||||
-- ^ \
|
||||
-- In both cases it touches both edges, but ends up outside in the
|
||||
-- top case, whereas it ends up inside in the left case. Just move
|
||||
-- the y co-ordinate down a smidge and it'll work properly, by
|
||||
-- missing one line in the left/right cases and hitting both/none
|
||||
-- in the top/bottom cases.
|
||||
--
|
||||
pY += 0.000001
|
||||
end if
|
||||
if pY<aY or pY>bY then return false end if
|
||||
if pX>max(aX,bX) then return false end if
|
||||
if pX<min(aX,bX) then return true end if
|
||||
if aX!=bX then
|
||||
m_red = (bY-aY)/(bX-aX)
|
||||
else
|
||||
m_red = inf
|
||||
end if
|
||||
if aX!=pX then
|
||||
m_blue = (pY-aY)/(pX-aX)
|
||||
else
|
||||
m_blue = inf
|
||||
end if
|
||||
return m_blue >= m_red
|
||||
end function
|
||||
|
||||
function inside(sequence point, sequence poly)
|
||||
integer in = 0
|
||||
for i=1 to length(poly) do
|
||||
if rayIntersectsSegment(point,poly[i]) then
|
||||
in = not in
|
||||
end if
|
||||
end for
|
||||
return in
|
||||
end function
|
||||
|
||||
constant INOUT = {"in", "out"}
|
||||
function in(integer flag, integer expected)
|
||||
if flag=expected then
|
||||
return INOUT[2-flag]
|
||||
end if
|
||||
return INOUT[2-flag] & "*** ERROR ***"
|
||||
end function
|
||||
|
||||
constant INSTAR = "* "
|
||||
function instar(integer flag)
|
||||
return INSTAR[2-flag]
|
||||
end function
|
||||
|
||||
constant test_points = {{5,5},{5,8},{-10,5},{0,5},{10,5},{8,5},{10,10}}
|
||||
|
||||
--constant NAME = 1, POLY = 2, EXPECTED = 3
|
||||
constant test_polygons = {
|
||||
{"square", {{{0,0},{10,0}},{{10,0},{10,10}},{{10,10},{0,10}},{{0,10},{0,0}}},
|
||||
{true,true,false,false,true,true,false}},
|
||||
{"square hole", {{{0,0},{10,0}},{{10,0},{10,10}},{{10,10},{0,10}},{{0,10},{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}}},
|
||||
{false,true,false,false,true,true,false}},
|
||||
{"strange", {{{0,5},{2.5,2.5}},{{2.5,2.5},{0,10}},{{0,10},{2.5,7.5}},{{2.5,7.5},{7.5,7.5}},
|
||||
{{7.5,7.5},{10,10}},{{10,10},{10,0}},{{10,0},{2.5,2.5}}},
|
||||
{true,false,false,false,true,true,false}},
|
||||
{"exagon", {{{3,0},{7,0}},{{7,0},{10,5}},{{10,5},{7,10}},{{7,10},{3,10}},{{3,10},{0,5}},{{0,5},{3,0}}},
|
||||
{true,true,false,false,true,true,false}}
|
||||
}
|
||||
|
||||
sequence name, poly, expected, tp
|
||||
|
||||
for i=1 to length(test_polygons) do
|
||||
{name,poly,expected} = test_polygons[i]
|
||||
printf(1,"\n%12s:",{name})
|
||||
for j=1 to length(test_points) do
|
||||
tp = test_points[j]
|
||||
printf(1," %s, %-4s",{sprint(tp),in(inside(tp,poly),expected[j])})
|
||||
end for
|
||||
end for
|
||||
|
||||
puts(1,"\n\n\n")
|
||||
|
||||
for i=0 to 10 do
|
||||
for j=1 to length(test_polygons) do
|
||||
puts(1," ")
|
||||
poly = test_polygons[j][2]
|
||||
for k=0 to 10 do
|
||||
puts(1,instar(inside({k+0.5,10.5-i},poly)))
|
||||
end for
|
||||
end for
|
||||
puts(1,"\n")
|
||||
end for
|
||||
Loading…
Add table
Add a link
Reference in a new issue