Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package proiect_geom;
- import java.io.FileNotFoundException;
- import java.util.Scanner;
- public class Proiect_geom {
- public static void main(String[] args) throws FileNotFoundException {
- Scanner in;
- in = new Scanner(new java.io.File("date.in"));
- int n = in.nextInt();
- Point[] polygon = new Point[n];
- for (int i = 0; i < n; i++) {
- double x, y;
- x = in.nextDouble();
- y = in.nextDouble();
- polygon[i] = new Point(x, y);
- }
- Point p = new Point(in.nextDouble(), in.nextDouble());
- triangulate(polygon);
- }
- static class Point implements Comparable {
- double x, y;
- Point(double x, double y) {
- this.x = x;
- this.y = y;
- }
- boolean between(Point p1, Point p2) {
- double minx = (p1.x < p2.x) ? p1.x : p2.x;
- double maxx = (p1.x > p2.x) ? p1.x : p2.x;
- double miny = (p1.y < p2.y) ? p1.y : p2.y;
- double maxy = (p1.y > p2.y) ? p1.y : p2.y;
- if ((this.x >= minx && this.x <= maxx) && (this.y >= miny && this.y <= maxy)) {
- return true;
- }
- return false;
- }
- @Override
- public int compareTo(Object o) {
- Point p = (Point) o;
- if (x > p.x) {
- return 1;
- } else if (x == p.x) {
- if (y > p.y) {
- return 1;
- } else if (y == p.y) {
- return 0;
- } else {
- return -1;
- }
- }
- return -1;
- }
- @Override
- public String toString() {
- return "Point{" + "x=" + x + ", y=" + y + '}';
- }
- public Point copy() {
- return new Point(x, y);
- }
- @Override
- public boolean equals(Object obj) {
- if (obj == null) {
- return false;
- }
- if (getClass() != obj.getClass()) {
- return false;
- }
- final Point other = (Point) obj;
- if (Double.doubleToLongBits(this.x) != Double.doubleToLongBits(other.x)) {
- return false;
- }
- if (Double.doubleToLongBits(this.y) != Double.doubleToLongBits(other.y)) {
- return false;
- }
- return true;
- }
- }
- static class Triangle {
- public final Point p1, p2, p3;
- Triangle(Point x, Point y, Point z) {
- p1 = x;
- p2 = y;
- p3 = z;
- }
- @Override
- public String toString() {
- return "Triangle{" + "p1=" + p1 + ", p2=" + p2 + ", p3=" + p3 + '}';
- }
- }
- //TODO
- private static boolean isConvex(Point p1, Point p2, Point p3)
- {
- if (area(p1,p2,p3) < 0)
- return true;
- else
- return false;
- }
- //TODO
- private static double area(Point p1,Point p2, Point p3)
- {
- double areaSum = 0;
- areaSum += p1.x * (-p3.y + p2.y);
- areaSum += p2.x * (-p1.y + p3.y);
- areaSum += p3.x * (-p2.y + p1.y);
- return areaSum;
- }
- static Triangle[] triangulate(Point[] polygon){
- boolean[] convex = new boolean[polygon.length];
- int n = polygon.length;
- convex[0] = isConvex(polygon[n-1],polygon[0],polygon[1]);
- for (int i = 1; i < n-1; i++){
- convex[i] = isConvex(polygon[i-1],polygon[i],polygon[i+1]);
- }
- convex[n-1] = isConvex(polygon[n-2],polygon[n-1],polygon[0]);
- System.out.println("Convex : ");
- for (int i = 0; i < n; i++){
- System.out.println(polygon[i] + " " +convex[i]);
- }
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement