-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvoronoiBox.m
77 lines (60 loc) · 1.34 KB
/
voronoiBox.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
function [ px,py,ccE,ee ] = voronoiBox( vx,vy,bound )
%FUNCTIONS Summary
% createSetEdges
% findBoxCorner
% findIntersctPoints
% shortenEdge
hh=createSetEdges(vx,vy);
ccE=findBoxCorner(bound);
[px,py,ee]=findIntersctPoints(hh,ccE);
end
function hh=createSetEdges(vx,vy)
for c=1:length(vx)
hh(c,:)=createEdge([vx(1,c), vy(1,c)],[vx(2,c), vy(2,c)]);
end
end
function cc=findBoxCorner(bound)
[mm nn]=meshgrid([bound(1) bound(2)],[bound(4) bound(3)]);
c1=cat(2,mm',nn');
c2=cat(2,mm,nn);
cc=[c1;c2];
end
function [px,py,hh]=findIntersctPoints(hh,cc)
% find intesection points
points=[];
for c=cc'
intersect=intersectEdges(hh, c');
[m,n]=find(~isnan(intersect));
index=unique(m);
points=[points;intersect(index,:)];
end
% get the matrix of intesected points
% p1 e1 e2 e3 e4 e5 e6 e7
% p2 e1 e2 e3 e4 e5 e6 e7
% p3 e1 e2 e3 e4 e5 e6 e7
% p4 e1 e2 e3 e4 e5 e6 e7
mat = isPointOnEdge(points, hh);
% shorten edge(s)
inde=1;
for rows=mat'
ndex=find(rows'==1);
hh(ndex,:)=shortenEdge(cc,hh(ndex,:),points(inde,:));
inde=inde+1;
end
px=points(:,1);
py=points(:,2);
end
function edge=shortenEdge(box,edge,point)
P1=edge(1:2);
P2=edge(3:4);
cc=[];
for ee=box'
e=reshape(ee',[2 2]);
cc=[cc;e];
end
if( isPointInPolygon(P1, cc))
edge=[P1 point];
else
edge=[point P2];
end
end