Voronoi diagram: Difference between revisions
Content added Content deleted
m (→{{header|Icon}} and {{header|Unicon}}: fixed taxi 3 and ref) |
|||
Line 208: | Line 208: | ||
[http://www.cs.arizona.edu/icon/library/src/procs/strings.icn strings.icn provides cat ] |
[http://www.cs.arizona.edu/icon/library/src/procs/strings.icn strings.icn provides cat ] |
||
=={{header| |
=={{header|Prolog}}== |
||
Works with SWI-Prolog and XPCE. <br> |
|||
3 Voronoi diagrams are given for the same sites, one with the Manhattan distance, one with the Euclidean distance and the last with the Minkowski distance (order 3). |
|||
<lang Prolog>:- dynamic pt/6. |
|||
voronoi :- |
|||
V is random(20) + 20, |
|||
retractall(pt(_,_,_,_)), |
|||
forall(between(1, V, I), |
|||
( X is random(390) + 5, |
|||
Y is random(390) + 5, |
|||
R is random(65535), |
|||
G is random(65535), |
|||
B is random(65535), |
|||
assertz(pt(I,X,Y, R, G, B)) |
|||
)), |
|||
voronoi(manhattan, V), |
|||
voronoi(euclide, V), |
|||
voronoi(minkowski_3, V). |
|||
voronoi(Distance, V) :- |
|||
sformat(A, 'Voronoi 400X400 ~w ~w', [V, Distance]), |
|||
new(D, window(A)), |
|||
send(D, size, size(400,400)), |
|||
new(Img, image(@nil, width := 400, height := 400 , kind := pixmap)), |
|||
forall(between(0,399, I), |
|||
forall(between(0,399, J), |
|||
( get_nearest_site(V, Distance, I, J, S), |
|||
pt(S, _, _, R, G, B), |
|||
send(Img, pixel(I, J, colour(@default, R, G, B)))))), |
|||
new(Bmp, bitmap(Img)), |
|||
send(D, display, Bmp, point(0,0)), |
|||
send(D, open). |
|||
get_nearest_site(V, Distance, I, J, S) :- |
|||
get_nearest_site(V, Distance, I, J, 0, 65535, S). |
|||
get_nearest_site(0, _Distance, _I, _J, S, _D, S). |
|||
get_nearest_site(V, Distance, I, J, S, D, SF) :- |
|||
pt(V, XV, YV, _, _, _), |
|||
call(Distance, I, J, XV, YV, DT), |
|||
( DT < D -> S1 = V, D1 = DT; S1 = S, D1 = D), |
|||
V1 is V - 1, |
|||
get_nearest_site(V1, Distance, I, J, S1, D1, SF). |
|||
manhattan(X1, Y1, X2, Y2, D) :- |
|||
D is abs(X2 - X1) + abs(Y2-Y1). |
|||
euclide(X1, Y1, X2, Y2, D) :- |
|||
D is sqrt((X2 - X1)**2 + (Y2-Y1)**2). |
|||
minkowski_3(X1, Y1, X2, Y2, D) :- |
|||
D is (abs(X2 - X1)**3 + abs(Y2-Y1)**3)**0.33. |
|||
</lang> |
|||
=={{header|PureBasic}}= |
|||
===Euclidean=== |
===Euclidean=== |