Advertisement
Sauka1337

Untitled

Oct 23rd, 2023
18
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.56 KB | None | 0 0
  1. program ConvexPolygonChecker;
  2.  
  3. Uses
  4. SysUtils;
  5.  
  6. Var
  7. N: Integer;
  8. IsIncorrect: Boolean;
  9. XValues: Array Of Integer;
  10. YValues: Array Of Integer;
  11. I: Integer;
  12. HasPositive, HasNegative: Boolean;
  13. CrossProduct: Double;
  14.  
  15. Begin
  16. Write('This program determines whether a polygon is convex based on the coordinates of its points entered by the user.');
  17. Repeat
  18. IsIncorrect := False;
  19. Write('Enter the number of sides of the polygon: ');
  20. Try
  21. ReadLn(N);
  22. If (N < 3) Then
  23. Begin
  24. WriteLn('Polygon must have at least 3 sides.');
  25. IsIncorrect := True;
  26. End
  27. Else If (N > 1000) Then
  28. Begin
  29. WriteLn('Number of sides is too big. Please, choose a value less than 1000.');
  30. IsIncorrect := True;
  31. End;
  32. Except
  33. WriteLn('Invalid input.');
  34. IsIncorrect := True;
  35. End;
  36. Until (Not IsIncorrect);
  37.  
  38. SetLength(XValues, N);
  39. SetLength(YValues, N);
  40.  
  41. For I := 0 To N - 1 Do
  42. Begin
  43. Repeat
  44. IsIncorrect := False;
  45.  
  46. Write('Enter x value for point ', I + 1, ': ');
  47. Try
  48. ReadLn(XValues[I]);
  49. if (XValues[I] < -1000) or (XValues[I] > 1000) Then
  50. Begin
  51. WriteLn('X value is out of range [-1000, 1000]. Please, choose a different value.');
  52. IsIncorrect := True;
  53. End;
  54. Except
  55. Begin
  56. WriteLn('Invalid input!');
  57. IsIncorrect := True;
  58. End;
  59. End;
  60.  
  61. Write('Enter y value for point ', I + 1, ': ');
  62. Try
  63. ReadLn(YValues[I]);
  64. If (YValues[I] < -1000) or (YValues[I] > 1000) then
  65. Begin
  66. WriteLn('Y value is out of range [-1000, 1000]. Please, choose a different value.');
  67. IsIncorrect := True;
  68. End;
  69. Except
  70. WriteLn('Invalid input!');
  71. IsIncorrect := True;
  72. End;
  73. Until (Not IsIncorrect);
  74. End;
  75.  
  76. HasPositive := False;
  77. HasNegative := False;
  78.  
  79. For I := 0 To N - 1 Do
  80. Begin
  81. CrossProduct := (XValues[(I + 1) mod N] - XValues[I]) * (YValues[(I + 2) mod N] - YValues[(I + 1) mod N]) -
  82. (YValues[(I + 1) mod N] - YValues[I]) * (XValues[(I + 2) mod N] - XValues[(I + 1) mod N]);
  83.  
  84. If CrossProduct > 0 Then
  85. HasPositive := True
  86. Else If CrossProduct < 0 Then
  87. HasNegative := True;
  88. End;
  89.  
  90. If (HasPositive And HasNegative) Or (Not HasPositive And Not HasNegative) Then
  91. Begin
  92. WriteLn('The polygon is not convex.');
  93. Readln;
  94. End
  95. Else
  96. WriteLn('The polygon is convex.');
  97. Readln;
  98. End.
  99.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement