HSPでドロネー三角形分割してみたよ

;
;[ Infomation ]
; Name :
; SubName :
; Version :
; copyright :
;
;[ Update history ]
;yyyy/mm/dd : ver : comment
;
;#ifndef __MODULE_NAME__
;#define global __MODULE_NAME__
#module
;---------------------------------------------------------------------------------------------------
;///////////////////////////////////////////////////////////////////////////////////////////////////
;
;
;
;#const global
;#enum global
;///////////////////////////////////////////////////////////////////////////////////////////////////
;
;
;
;[ Infomation ]
; GetCircumcircle double ptx1, double pty1, double ptx2, double pty2, double ptx3, double pty3, var resPtx, var resPty, var resR
; double ptx1, double pty1 : [IN] 1x,y
; double ptx2, double pty2 : [IN] 2x,y
; double ptx3, double pty3 : [IN] 3x,y
; var resPtx, var resPty : [OUT]
; var resR : [OUT]
;
; return :
;
;[ comment ]
;
;
; stat
;
; stat = 0 :
;
;
; stat = 1 :
;
;
; resR 0.0
;
#deffunc GetCircumcircle double ptx1, double pty1, double ptx2, double pty2, double ptx3, double pty3, var resPtx, var resPty, var resR
;resPt
;resR
;
;2 * x * (x2-x1) + 2 * y * (y2-y1) = (x2*x2 - x1*x1 + y2*y2 - y1*y1)
;2 * x * (x3-x1) + 2 * y * (y3-y1) = (x3*x3 - x1*x1 + y3*y3 - y1*y1)
;
;2*x * a + 2*y * b = c
;2*x * d + 2*y * e = f
;
a = ptx2 - ptx1
d = ptx3 - ptx1
b = pty2 - pty1
e = pty3 - pty1
c = ptx2*ptx2 - ptx1*ptx1 + pty2*pty2 - pty1*pty1
f = ptx3*ptx3 - ptx1*ptx1 + pty3*pty3 - pty1*pty1
;
; 3
;
n = 2.0 * ( b*d - a*e)
if n = 0.0 {
;
resPty = ptx1
resPtx = pty1
resR = 0.0
return 1
}
;
;
;
resPty = (c*d - a*f) / n
if a ! 0.0 {
resPtx = (0.5*c - b*resPty) / a
} else {
; ad0.0
resPtx = (0.5*f - e*resPty) / d
}
;
;
;
x = resPtx - ptx1
y = resPty - pty1
resR = sqrt( x * x + y * y )
return 0
;///////////////////////////////////////////////////////////////////////////////////////////////////
;
;
;
;[ Infomation ]
; GetHugeTriangle double ltx, double lty, double rux, double ruy, var tx0, var ty0, var tx1, var ty1, var tx2, var ty2
; double ltx : X
; double lty : Y
; double rux : X
; double ruy : Y
; tx0, ty0 : 0
; tx1, ty1 : 1
; tx2, ty2 : 2
;
; return : 0
;
;[ comment ]
;(ltx, lty)-(rux, ruy)
;
;
#deffunc GetHugeTriangle double ltx, double lty, double rux, double ruy, var tx0, var ty0, var tx1, var ty1, var tx2, var ty2
hw = (rux - ltx)/2.0
hh = (ruy - lty)/2.0
r = sqrt( hw * hw + hh * hh )
r3 = r * sqrt(3)
tx0 = ltx + hw - r3
ty0 = lty + hh - r
tx1 = ltx + hw
ty1 = lty + hh + r*2
tx2 = rux - hw + r3
ty2 = ty0
return
;-------------------------------------------------------------------------------
;---------------------------------------------------------------------------------------------------
#global
;#endif ;__MODULE_NAME__
;#################################################
;
;triangle
;circumcircle
;
#module modTriangle tri0, tri1, tri2, ccm_x, ccm_y, ccm_r
;------------------------
;
;
#modinit int t0, int t1, int t2, array pts
tri0 = t0
tri1 = t1
tri2 = t2
;
GetCircumcircle pts( 0, t0), pts( 1, t0), pts( 0, t1), pts( 1, t1), pts( 0, t2), pts( 1, t2), xx, yy, rr
ccm_x = xx
ccm_y = yy
ccm_r = rr
return
;------------------------
;
;
#modterm
; mes ""
return
;------------------------
; 調
;0:
;1:
#modfunc modTri_IsInsideCircumcircle double ptx, double pty
x = ptx - ccm_x
y = pty - ccm_y
if (x*x + y*y) < ccm_r*ccm_r : return 1 ;
return 0 ;
;------------------------
;
#modfunc modTri_GetPoints var tt0, var tt1, var tt2
;
tt0 = tri0
tt1 = tri1
tt2 = tri2
return
;------------------------
;
;
;
#modfunc modTri_IsEquals int t0, int t1, int t2
f = (tri0 = t0) & (tri1 = t1) & (tri2 = t2)
f |= (tri0 = t0) & (tri1 = t2) & (tri2 = t1)
f |= (tri0 = t1) & (tri1 = t0) & (tri2 = t2)
f |= (tri0 = t2) & (tri1 = t0) & (tri2 = t1)
f |= (tri0 = t1) & (tri1 = t2) & (tri2 = t0)
f |= (tri0 = t2) & (tri1 = t1) & (tri2 = t0)
return f
#global
;#################################################
;
;
#module
;------------------------
;
#deffunc DelaunayIni
ddim points, 2, 1000
pointsCount = 0 ;
;
;
pointAreaX = 0
pointAreaY = 0
pointAreaW = ginfo_winx
pointAreaH = ginfo_winy
;
GetHugeTriangle pointAreaX, pointAreaY, pointAreaX + pointAreaW - 1, pointAreaY + pointAreaH - 1, tx0, ty0, tx1, ty1, tx2, ty2
points( 0, 0) = double(tx0), double(ty0)
points( 0, 1) = double(tx1), double(ty1)
points( 0, 2) = double(tx2), double(ty2)
pointsCount = 3
;
newmod Triangle, modTriangle, 0,1,2, points
return
;------------------------
;
#deffunc DelaunayAddPoint double tx, double ty
;
points( 0, pointsCount) = tx, ty
pc = pointsCount
pointsCount++
;
; 調
;
;
foreach Triangle
modTri_IsInsideCircumcircle Triangle(cnt), tx, ty
if stat {
;
;
modTri_GetPoints Triangle(cnt), t0, t1, t2 ;
delmod Triangle(cnt)
;
;
newmod TriangleSub, modTriangle, pc, t0, t1, points
newmod TriangleSub, modTriangle, pc, t1, t2, points
newmod TriangleSub, modTriangle, pc, t2, t0, points
}
loop
;
;
foreach TriangleSub
modTri_GetPoints TriangleSub(cnt), t0, t1, t2
i = cnt
foreach TriangleSub
;
if i ! cnt {
modTri_IsEquals TriangleSub(cnt), t0, t1, t2
if stat {
delmod TriangleSub(cnt)
delmod TriangleSub(i)
;mes "("+cnt+", "+i+")"+t0+", "+t1+", "+t2
}
}
loop
loop
;
;
foreach TriangleSub
modTri_GetPoints TriangleSub(cnt), t0, t1, t2
newmod Triangle, modTriangle, t0, t1, t2, points
;
delmod TriangleSub(cnt)
loop
return pointsCount
;------------------------
;
#deffunc DelaunayDraw
dim gx, 4
dim gy, 4
dim gc, 4
gc = $FF0000, $00FF00, $0000FF, $0000FF
;
r = 2
repeat pointsCount
x = points( 0, cnt)
y = points( 1, cnt)
circle x-r, y-r, x+r, y+r, 1
pos x, y
;mes ""+cnt
loop
;
;
i = 0
foreach Triangle
modTri_GetPoints Triangle(cnt), t0, t1, t2
;
if (t0 = 0)|(t0 = 1)|(t0 = 2) | (t1 = 0)|(t1 = 1)|(t1 = 2) | (t2 = 0)|(t2 = 1)|(t2 = 2) : continue
;
gx = int(points( 0, t0)), int(points( 0, t1)), int(points( 0, t2)), int(points( 0, t2))
gy = int(points( 1, t0)), int(points( 1, t1)), int(points( 1, t2)), int(points( 1, t2))
;gsquare -257, gx, gy, gc
;
line points( 0, t0), points( 1, t0), points( 0, t1), points( 1, t1)
line points( 0, t1), points( 1, t1), points( 0, t2), points( 1, t2)
line points( 0, t2), points( 1, t2), points( 0, t0), points( 1, t0)
i++
loop
pos 0,0
mes ""+i
return
#global
;#################################################
;-------------------------------------------------------------------------------
;
;
;
;#####################################################################
;
;
;
;0 #include
;1
#if 1
;
DelaunayIni
#define POINT_MAX 50
;
;
;
randomize
repeat POINT_MAX
; x = double( rnd( ginfo_winx/2 )+ginfo_winx/4 )
; y = double( rnd( ginfo_winy/2 )+ginfo_winy/4 )
x = double( rnd( ginfo_winx/2 ) )*2.0
y = double( rnd( ginfo_winy/2 ) )*2.0
DelaunayAddPoint x, y
loop
;
DelaunayDraw
#endif
;#####################################################################
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX