1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
| //de p1 à p4 ce sont les coins du carré
Point2D p1 = new Point2D.Double(b.getX(), b.getY());
Point2D p2 = new Point2D.Double(b.getX()+b.getWidth(), b.getY());
Point2D p3 = new Point2D.Double(b.getX()+b.getWidth(), b.getY()+b.getHeight());
Point2D p4 = new Point2D.Double(b.getX(), b.getY()+b.getHeight());
//e est le centre d'ellipse
double r = ellipse.getWidth()/2;
Point2D e = new Point2D.Double(ellipse.getX() + r, ellipse.getY() + r);
//de d1 à d4 c'est la distance du centre d'ellipse au coins du carré
double d1 = e.distance(p1);
double d2 = e.distance(p2);
double d3 = e.distance(p3);
double d4 = e.distance(p4);
//de t2 à t4 c'est les futurs coins du polygon
Point2D t1 = new Point2D.Double();
Point2D t2 = new Point2D.Double();
Point2D t3 = new Point2D.Double();
Point2D t4 = new Point2D.Double();
//là je dit, si le coté le plus proche du centre d'ellipse est p1(correspondance avec d1), alors:
if(d1<=d2 && d1<=d3 && d1<=d4){
t1 = new Point2D.Double(p2.getX()*(r/d2), p2.getY()*(r/d2));//p2
t2 = new Point2D.Double(p4.getX()*(r/d4), p4.getY()*(r/d4));//p4
t3 = p4;
t4 = p2;
}else if(d2<=d1 && d2<=d3 && d2<=d4){
t1 = new Point2D.Double(p3.getX()*(r/d3), p3.getY()*(r/d3));//p3
t2 = new Point2D.Double(p1.getX()*(r/d1), p1.getY()*(r/d1));//p1
t3 = p1;
t4 = p3;
}else if(d3<=d1 && d3<=d2 && d3<=d4){
t1 = new Point2D.Double(p4.getX()*(r/d4), p4.getY()*(r/d4));//p4
t2 = new Point2D.Double(p2.getX()*(r/d2), p2.getY()*(r/d2));//p2
t3 = p2;
t4 = p4;
}else if(d4<=d1 && d4<=d2 && d4<=d3){
t1 = new Point2D.Double(p1.getX()*(r/d1), p1.getY()*(r/d1));//p1
t2 = new Point2D.Double(p3.getX()*(r/d3), p3.getY()*(r/d3));//p3
t3 = p1;
t4 = p3;
}
//tout ca on s'ent fou, juste je met dans un Area et je return, et Arc2D c'est pour plus tard
Area returnArea = new Area();
Arc2D arc = this.createArc2D(t1, t2, new Point2D.Double(ellipse.getX(), ellipse.getY()));
Area area = new Area(arc);
Polygon poly = new Polygon();
//this.add(area);
poly.addPoint((int)t1.getX(), (int)t1.getY());
poly.addPoint((int)t2.getX(), (int)t2.getY());
poly.addPoint((int)t3.getX(), (int)t3.getY());
poly.addPoint((int)t4.getX(), (int)t4.getY());
area = new Area(poly);
returnArea.add(area);
return returnArea;
}
public void addDarkness(){
this.reset();
this.add(new Area(new Rectangle2D.Double(0, 0, p.getScreenWidth(), p.getScreenHeight())));
}
public Arc2D createArc2D(Point2D p1, Point2D p2, Point2D center){
Point2D middle = new Point2D.Double((p1.getX()+p2.getX())/2, (p1.getY()+p2.getY())/2);
Double radius = p1.distance(center);
Double distP1 = Math.sqrt(Math.pow(p1.getX(), 2) + Math.pow(p1.getY(), 2));
Double distP2 = Math.sqrt(Math.pow(p2.getX(), 2) + Math.pow(p2.getY(), 2));
double angSt;
double angExt;
if(distP1<distP2){
angSt = angle(center, p1);
angExt = angle(center, p2)-angSt;
}else{
angSt = angle(center, p2);
angExt = angle(center, p1)-angSt;
}
if(center.getX()>middle.getX()){ angExt+=360;}
Arc2D arc2D = new Arc2D.Double();
arc2D.setArcByCenter(center.getX(), center.getY(), radius, angSt, angExt, Arc2D.OPEN);
return arc2D; |
Partager