Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- program ConvexPolygonChecker;
- Uses
- SysUtils;
- Var
- N: Integer;
- IsIncorrect: Boolean;
- XValues: Array Of Integer;
- YValues: Array Of Integer;
- I: Integer;
- HasPositive, HasNegative: Boolean;
- CrossProduct: Double;
- Begin
- Write('This program determines whether a polygon is convex based on the coordinates of its points entered by the user.');
- Repeat
- IsIncorrect := False;
- Write('Enter the number of sides of the polygon: ');
- Try
- ReadLn(N);
- If (N < 3) Then
- Begin
- WriteLn('Polygon must have at least 3 sides.');
- IsIncorrect := True;
- End
- Else If (N > 1000) Then
- Begin
- WriteLn('Number of sides is too big. Please, choose a value less than 1000.');
- IsIncorrect := True;
- End;
- Except
- WriteLn('Invalid input.');
- IsIncorrect := True;
- End;
- Until (Not IsIncorrect);
- SetLength(XValues, N);
- SetLength(YValues, N);
- For I := 0 To N - 1 Do
- Begin
- Repeat
- IsIncorrect := False;
- Write('Enter x value for point ', I + 1, ': ');
- Try
- ReadLn(XValues[I]);
- if (XValues[I] < -1000) or (XValues[I] > 1000) Then
- Begin
- WriteLn('X value is out of range [-1000, 1000]. Please, choose a different value.');
- IsIncorrect := True;
- End;
- Except
- Begin
- WriteLn('Invalid input!');
- IsIncorrect := True;
- End;
- End;
- Write('Enter y value for point ', I + 1, ': ');
- Try
- ReadLn(YValues[I]);
- If (YValues[I] < -1000) or (YValues[I] > 1000) then
- Begin
- WriteLn('Y value is out of range [-1000, 1000]. Please, choose a different value.');
- IsIncorrect := True;
- End;
- Except
- WriteLn('Invalid input!');
- IsIncorrect := True;
- End;
- Until (Not IsIncorrect);
- End;
- HasPositive := False;
- HasNegative := False;
- For I := 0 To N - 1 Do
- Begin
- CrossProduct := (XValues[(I + 1) mod N] - XValues[I]) * (YValues[(I + 2) mod N] - YValues[(I + 1) mod N]) -
- (YValues[(I + 1) mod N] - YValues[I]) * (XValues[(I + 2) mod N] - XValues[(I + 1) mod N]);
- If CrossProduct > 0 Then
- HasPositive := True
- Else If CrossProduct < 0 Then
- HasNegative := True;
- End;
- If (HasPositive And HasNegative) Or (Not HasPositive And Not HasNegative) Then
- Begin
- WriteLn('The polygon is not convex.');
- Readln;
- End
- Else
- WriteLn('The polygon is convex.');
- Readln;
- End.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement