#Trying to connect points on a JFrame using a Line Segment Class, but failing at doing so.
28 messages · Page 1 of 1 (latest)
Hey, @thorny talon!
Please remember to /close this post once your question has been answered!
<@&765578700724371486>
graphics?
paint components
Yeah.
So, I'm trying to create a program that will essentially take a list of points from a Text File, generate a polygon out of those points, and determine the shape and print it to the user.
use this
Ok, so I actually managed to fix the reason as to why it wasn't drawing the line segment.
But now I want to try and improve my Algorithm for determining the Shape.
Since I'm wondering if the way it determines the Shape might always result in a convex polygon being the result.
Would it be better for me to determine it in a different way?
make a while loop and ask for polygon x, y
We're not allowed to use a Polygon class in the requirements.
if player input is "generate" then stop loop and drae
At least, based off the UML Diagram.
so how do you wanna draw?
The way we do it is by having an Array of Points, and a GeometricObject Array, both of which will have equal values.
We then pass drawn line segments within the GraphDisplay class into the GeometricObject Array so it displays and creates a shape with the points.
I just want to try figuring out a new way of determining the shape that would be more reliable.
public class Algorithms{
public static String polygonType(Point[] points){
if(points.length < 3) return "A Point or Line Segment is not enough to determine a Polygon.";
if(points.length == 3) return "The shape is a Triangle.";
boolean convexPoly = true;
boolean rectilinearPoly = true;
for(int i = 1; i < points.length - 1; i++){
double angleMeasurement = 0;
if(i == points.length) angleMeasurement = points[i].angleCalc(points[i-1], points[i], points[0]);
else angleMeasurement = points[i].angleCalc(points[i-1], points[i], points[i+1]);
if(angleMeasurement != 90) rectilinearPoly = false;
if(angleMeasurement >= 180) convexPoly = false;
}
if(points.length == 4 && rectilinearPoly == true) return "The shape is a Rectangle.";
else if(rectilinearPoly == true) return "The shape is a Rectlinear Polygon.";
else if(convexPoly == true) return "The shape is a Convex Polygon.";
else return "The shape is a Simple Polygon.";
}
}```
public double angleCalc(Point p1, Point center, Point p2){
return (Math.atan2(p2.getY() - center.getY(), p2.getX() - center.getX()) - Math.atan2(p1.getY() - center.getY(), p1.getX() - center.getX()));
}```