Browse Source

feat: début d'algorithme PIP

Martin Passard 6 years ago
parent
commit
5c06f452fc
4 changed files with 170 additions and 5 deletions
  1. 19 5
      src/data/CoveredAreas.java
  2. 10 0
      src/data/PointInPoligonTest.java
  3. 84 0
      src/data/Polygon.java
  4. 57 0
      src/data/Segment.java

+ 19 - 5
src/data/CoveredAreas.java

@@ -13,12 +13,20 @@ public class CoveredAreas {
 	private final List<TechnoCoverage> technos;
 	private final String name;
 	private ISP isp;
+	private Polygon poligon;
 	
-	public CoveredAreas(String name, ISP isp, List<TechnoCoverage> techno) {
-		technos=techno;
-		this.name=name;
+
+
+	public CoveredAreas(String name, ISP isp,List<TechnoCoverage> techno, Polygon poligon) {
+		super();
+		this.technos = techno;
+		this.name = name;
 		this.isp = isp;
-		
+		this.poligon = poligon;
+	}
+
+	public CoveredAreas(String name, ISP isp, List<TechnoCoverage> techno) {
+		this(name,isp,techno,null);
 	}
 	
 	public CoveredAreas(String name, List<TechnoCoverage> techno) {
@@ -63,7 +71,13 @@ public class CoveredAreas {
 		this.isp = isp;
 	}
 	
-	
+	public Polygon getPoligon() {
+		return poligon;
+	}
+
+	public void setPoligon(Polygon poligon) {
+		this.poligon = poligon;
+	}
 	
 
 }

+ 10 - 0
src/data/PointInPoligonTest.java

@@ -0,0 +1,10 @@
+package data;
+
+public class PointInPoligonTest {
+
+	public static void main(String[] args) {
+		// TODO Auto-generated method stub
+
+	}
+
+}

+ 84 - 0
src/data/Polygon.java

@@ -0,0 +1,84 @@
+package data;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.LinkedList;
+import java.util.List;
+
+public class Polygon {
+	private List<Coordinates> points;
+	
+	public Polygon(){
+		points = new ArrayList<>();
+	}
+	
+	public Polygon(List<Coordinates> points) {
+		this.points = points;
+	}
+	
+	public boolean addPoint(Coordinates point) {
+		return this.points.add(point);
+	}
+	
+	
+
+	public boolean isEmpty() {
+		return points.isEmpty();
+	}
+
+	public boolean add(Coordinates e) {
+		return points.add(e);
+	}
+
+	public boolean addAll(Collection<? extends Coordinates> c) {
+		return points.addAll(c);
+	}
+
+	public boolean addAll(int index, Collection<? extends Coordinates> c) {
+		return points.addAll(index, c);
+	}
+
+	public void add(int index, Coordinates element) {
+		points.add(index, element);
+	}
+
+	public List<Coordinates> getPoints() {
+		return points;
+	}
+
+	public void setPoints(List<Coordinates> points) {
+		this.points = points;
+	}
+	
+	public int getPointsNumber() {
+		return points.size();
+	}
+	
+	public Segment getSegment(int id) {
+		int size = points.size();
+		return new Segment(points.get(id%size),points.get((id+1)%size));
+	}
+	
+	public List<Segment> getAllSegmentsOfThePolygonForTheLatitude(double latitude){
+		List<Segment> res = new LinkedList<>();
+		for(int i=0; i<points.size(); ++i) {
+			Segment s = getSegment(i);
+			if(s.isLatitudeBeetweenTheTwoPoints(latitude)) {
+				res.add(s);
+			}
+		}
+		return res;
+	}
+	
+	public boolean isInPolygon(Coordinates coordinates) {
+		List<Segment> segmentsInTheLatitude =  getAllSegmentsOfThePolygonForTheLatitude(coordinates.getLatitude());
+		int segmentsCrossed = 0;
+		for(Segment s : segmentsInTheLatitude) {
+			double longitude = s.getLongitudeCorrespondingToTheLatitude(coordinates.getLatitude());
+			if(longitude<=coordinates.getLongitude()) segmentsCrossed++;
+		}
+		return (segmentsCrossed / 2) == 1;  //nombre impair de croisement de segments: on est dans le polygone.
+	}
+	
+	
+}

+ 57 - 0
src/data/Segment.java

@@ -0,0 +1,57 @@
+package data;
+
+/**
+ * Cette classe représente un segment composé de 2 coordonées.(points)
+ * @author marmat
+ *
+ */
+public class Segment {
+	private Coordinates c1,c2;
+	
+	public Segment(Coordinates c1 , Coordinates c2) {
+		this.c1 = c1;
+		this.c2 = c2;
+	}
+	
+	public boolean isLongitudeBeetweenTheTwoPoints(double longitude) {
+		boolean b1 = (longitude >= c1.getLongitude()) && (longitude <= c2.getLongitude());
+		boolean b2 = (longitude <= c1.getLongitude()) && (longitude >= c2.getLongitude());
+		return b1 || b2;
+	}
+	
+	public boolean isLatitudeBeetweenTheTwoPoints(double latitude) {
+		boolean b1 = (latitude >= c1.getLatitude()) && (latitude <= c2.getLatitude());
+		boolean b2 = (latitude <= c1.getLatitude()) && (latitude >= c2.getLatitude());
+		return b1 || b2;
+	}
+	/**
+	 * Longueur du segment. Utilise Coordinate.distanceAvec avec les deu paramètres.
+	 * @return distance
+	 */
+	public double length() {
+		return c1.distanceAvec(c2);
+	}
+	
+	/**
+	 * Regarde quelle valeur de longitude on a sion place un point à une latitude sur le segment
+	 * @param latitude latitude qui est censée se situer entre celle des deux points.
+	 * @return longitude correspondante a la latitude sur le segment.
+	 */
+	public double getLongitudeCorrespondingToTheLatitude(double latitude) {
+		Coordinates pointsTriangle;
+		if(c1BiggestLat()) {
+			pointsTriangle = new Coordinates(c1.getLatitude(), c2.getLongitude());
+		}else {
+			pointsTriangle = new Coordinates(c2.getLatitude(), c1.getLongitude());
+		}
+		double distPtC1=pointsTriangle.distanceAvec(c1);
+		double distPtC2=pointsTriangle.distanceAvec(c2);
+		double rapport = new Coordinates(latitude,c2.getLongitude()).distanceAvec(c2)/distPtC2;
+		return c2.getLatitude()+(rapport*(distPtC1));
+	}
+	
+	public boolean c1BiggestLat() {
+		return c1.getLatitude()>=c2.getLatitude();
+	}
+
+}