Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Uses Graph, Math;
- Type TPoint = Record
- X, Y: Integer;
- End;
- Type TPolygon = Array of TPoint;
- // Point
- Function Point(const X, Y: Integer): TPoint;
- Begin
- Result.X := X;
- Result.Y := Y;
- End;
- // draw_polygon
- Procedure draw_polygon(const Poly: TPolygon);
- Var L, H, I: Integer;
- Begin
- L := Low(Poly);
- H := High(Poly);
- Line(Poly[L].X, Poly[L].Y, Poly[H].X, Poly[H].Y);
- For I := L To H-1 Do
- Line(Poly[I].X, Poly[I].Y, Poly[I+1].X, Poly[I+1].Y);
- For I := L To H Do
- Circle(Poly[I].X, Poly[I].Y, 5);
- End;
- // insert_point
- Procedure insert_point(var Poly: TPolygon; const Point: TPoint; const Pos: Integer);
- Var I: Integer;
- Begin
- SetLength(Poly, Length(Poly)+1);
- For I := High(Poly) Downto Pos+1 Do
- Poly[I] := Poly[I-1];
- Poly[Pos] := Point;
- End;
- // polygon_smooth
- Function polygon_smooth(const Poly: TPolygon): TPolygon;
- // getDistance
- Function getDistance(const A, B: TPoint): Integer; inline;
- Begin
- Exit(Round(sqrt(sqr(A.X-B.X) + sqr(A.Y-B.Y))));
- End;
- // main block
- Var OutPoly : TPolygon;
- I, NewPoints: Integer;
- P1, P2, P3, P4: TPoint;
- Begin
- SetLength(OutPoly, Length(Poly));
- For I := Low(OutPoly) To High(OutPoly) Do
- Begin
- OutPoly[I].X := Poly[I].X;
- OutPoly[I].Y := Poly[I].Y;
- End;
- NewPoints := 1;
- I := 0;
- Repeat
- P1 := Poly[I];
- P2 := Poly[(I+1) mod Length(Poly)];
- insert_point(OutPoly, Point(
- (P1.X+P2.X) div 2,
- (P1.Y+P2.Y) div 2
- ), I+NewPoints);
- Inc(NewPoints);
- Inc(I);
- Until (I > High(Poly));
- I := 1;
- Repeat
- P1 := OutPoly[I];
- P2 := OutPoly[(I+1) mod Length(OutPoly)];
- P3 := OutPoly[(I+2) mod Length(OutPoly)];
- P4.X := (P1.X+P3.X) div 2;
- P4.Y := (P1.Y+P3.Y) div 2;
- P4.X := (P4.X+P2.X) div 2;
- P4.Y := (P4.Y+P2.Y) div 2;
- With OutPoly[(I+1) mod Length(OutPoly)] do
- Begin
- X := P4.X;
- Y := P4.Y;
- End;
- Inc(I, 2);
- Until (I > High(OutPoly));
- Exit(OutPoly);
- End;
- Var GD, GM: Smallint;
- Poly : TPolygon;
- Begin
- GD := 0;
- GM := 0;
- InitGraph(GD, GM, '2D polygon smooth');
- SetLength(Poly, 3);
- Poly[0] := Point(100, 600);
- Poly[1] := Point(600, 100);
- Poly[2] := Point(1100, 600);
- SetColor(White);
- draw_polygon(Poly);
- Poly := polygon_smooth(Poly);
- SetColor(Red);
- draw_polygon(Poly);
- Poly := polygon_smooth(Poly);
- SetColor(Green);
- draw_polygon(Poly);
- Poly := polygon_smooth(Poly);
- SetColor(Blue);
- draw_polygon(Poly);
- Readln;
- End.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement