I need to draw a polygon in Pascal(dev-pascal) which has semicircles at its sides. I’ll give the code I have come up so far. It has diagonals too.
uses crt,graph;
var a,b:smallint;
x0,y0,n,j,k,r:integer;
xevi,yoni:array[1..50] of integer;
i:real;
begin
write('n?.. ');readln(n);
detectgraph(a,b);
initgraph(a,b,'');
x0:=getmaxx div 2;
y0:=getmaxy div 2;
j:=0;
i:=0;
repeat
inc(j);
xevi[j]:=trunc( x0+200*cos(i) );
yoni[j]:=trunc( y0-200*sin(i) );
i:=i+2*pi/n;
until i>2*pi;
r:=trunc( sqrt( sqr((xevi[1]-xevi[2])) + sqr((yoni[1]-yoni[2])))) div 2;
for j:=1 to n do begin
if (xevi[j+1]-xevi[j])<>0 then begin
k:=trunc( arctan(
//abs(
((yoni[j+1]-yoni[1])/(xevi[j+1]-xevi[j])-1)
/(1+(yoni[j+1]-yoni[1])/(xevi[j+1]-xevi[j]))
//)
)*180/pi); end
else k:=90;
if k>0 then arc( (xevi[j]+xevi[j+1]) div 2, (yoni[j]+yoni[j+1]) div 2, k, k+180, r )
else begin
k:=360+k;
if k+180>360 then k:=360-k;
arc( (xevi[j]+xevi[j+1]) div 2, (yoni[j]+yoni[j+1]) div 2, k, k+180, r );
end;
end;
for j:=1 to n do
for k:=1 to n do
line( xevi[j],yoni[j],xevi[k],yoni[k]);
readln;
closegraph;
end.
As you can see, I tried using analytic geometry to find the slope between two adjacent vertices of the polygon, then calculating the angle of a line with that slope, and then using the angle to draw an arc.
So basically, I don’t know why it doesn’t work, and I’m also sure there’s a much simpler way to do it! Any help at all will be appreciated!
Thank you!
i don’t know pascal so will just use pseudo-code.
given that, the call to arc should be either:
or
and i think you shouldn’t need to adjust for different angles.
this is very similar to what you have, except that you have some strange
+1and-1values in your atan2, and you shouldn’t need to worry so much about the angle.