% script to interactively build polygons with % set up window clf plot([-1,1,1,-1,-1], [-1,-1,1,1,-1], 'b-') axis([-1,1,-1,1]); axis equal % 1) input coordinates of polygon vertices with mouse button = 1; nverts = 1; x = [], y = []; while(button<4) plot([-1,1,1,-1,-1], [-1,-1,1,1,-1], 'b-') hold on if(nverts>1) scatter(x,y,'r*'); end axis([-1,1,-1,1]); axis equal title('Click to select vertex (c to continue)', 'FontSize', 16) [xn,yn,button] = ginput(1); if(button<4) x(nverts) = xn; y(nverts) = yn; nverts = nverts+1; end end nverts = nverts-1; % 2) select edges as pairs of vertices with mouse edges = []; button = 1; while(button<4) plot([-1,1,1,-1,-1], [-1,-1,1,1,-1], 'b-') hold on if(nverts>1) scatter(x,y,'r*'); end if(~isempty(edges)) plot(x(edges)',y(edges)', 'k-') end axis([-1,1,-1,1]); axis equal title('choose first vertex (c to continue)', 'FontSize', 16) [x1,y1,button] = ginput(1); [mind, n1] = min( (x-x1).^2 + (y-y1).^2 ); if(button=='c') break; end hold on; scatter(x(n1),y(n1), 'bo', 'filled') hold off title('choose second vertex (c to continue)', 'FontSize', 16) [x2,y2,button] = ginput(1); [mind, n2] = min( (x-x2).^2 + (y-y2).^2 ); if(button=='c') break; end hold on; scatter(x(n2),y(n2), 'bo', 'filled') plot([x(n1),x(n2)],[y(n1),y(n2)], 'k-'); hold off title('space bar to reject, a to accept, c to continue', 'FontSize', 16) [foo,bah,button] = ginput(1); if(button==' ') end if(button=='a' || button=='c') [n1,n2] edges = [edges;[n1,n2]] end if(button=='c') break; end button = 0; end % 3) choose edges that form polygon with mouse nedges = size(edges, 1); polyg = []; button = 1; while(button<4) plot([-1,1,1,-1,-1], [-1,-1,1,1,-1], 'b-') hold on scatter(x,y,'r*'); plot(x(edges),y(edges), 'k-') if(~isempty(polyg)) plot(x(edges(polyg,:))',y(edges(polyg,:))', 'b-')' end hold off axis([-1,1,-1,1]); axis equal title('choose edge (c to continue)', 'FontSize', 16) [mx,my,button] = ginput(1); mx = [mx;my]; ab = zeros(size(edges,1),1); for e=1:size(edges,1) v1 = edges(e,1); v2 = edges(e,2); x1 = [x(v1);y(v1)]; x2 = [x(v2);y(v2)]; b = dot(mx-x1,x2-x1)/norm(x2-x1)^2; b = max(min(b,1),0); ab(e) = norm( mx - (x1 + b*(x2-x1)) ); end [mind, e] = min( ab ); if(button=='c') break; end hold on plot(x(edges(e,:))',y(edges(e,:))', 'b-') hold off title('a Accept (c to continue)', 'FontSize', 16) [foo,bah,button] = ginput(1); if(button=='a') polyg = [polyg;e]; end if(button=='c') break; end button = 0; pause(.04) end % 4) select point inside polygon with mouse button = 'b'; while(button~='a') title('select point inside polygon', 'FontSize', 16) [xin,yin] = ginput(1); hold on scatter(xin,yin, 'k*'); title('a accept', 'FontSize', 16); [foo,bah,button] = ginput(1); end % 5) output polygon to .poly formatted file fname = input('filename: ', 's'); fid = fopen(fname, 'w'); fprintf(fid, '# %s example \n', fname); fprintf(fid, '# Number of vertices\n'); fprintf(fid, '%d \n', nverts); fprintf(fid, '# Vertex descriptions [vertex number, x coordinate, y coordinate]\n'); for n=1:nverts fprintf(fid, '%d %f %f\n', n, x(n),y(n)); end fprintf(fid, '# Number of edges \n'); fprintf(fid, '%d\n', nedges); fprintf(fid, '# Edge descriptions [edge number, first vertex, second vertex]\n'); for n=1:nedges fprintf(fid, '%d %d %d \n', n, edges(n,1), edges(n,2)); end fprintf(fid, '# Number of edges in polygon \n'); fprintf(fid, '%d\n', length(polyg)); fprintf(fid, '# Polygon description [ list of edges ]\n'); for n=1:length(polyg) fprintf(fid, '%d ', polyg(n)); end fprintf(fid, '\n'); fprintf(fid, '# Point inside polygon [ x coordinate, y coordinate ]\n'); fprintf(fid, '%f %f\n', xin,yin); fclose(fid);