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 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
|
import javax.swing.*;
import java.awt.geom.*;
import java.awt.*;
import java.util.*;
public class Graph extends JPanel {
ArrayList<Points> tab1 = new ArrayList<Points>();
ArrayList<Points> tab2 = new ArrayList<Points>();;
Path2D.Double path,path2;
ArrayList<Integer> list = new ArrayList<Integer>();
ArrayList<Points> inter = new ArrayList<Points>();
ArrayList<Path2D.Double> polygones = new ArrayList<Path2D.Double>();
public Graph() {
//creation des tableau de points (tab1 = cos avec une echelle qui permet de voir, tab2 = sin
for(int i = 0; i<100;i++) {
tab1.add(new Points(i*4*Math.PI/100.0,Math.cos(i*4*Math.PI/100.0)));
tab2.add(new Points(i*4*Math.PI/100,Math.sin(i*4*Math.PI/100)));
}
//utilisation de Path2D(a la place de line2D). on trace la première ligne de chaque tab
path = new Path2D.Double(new Line2D.Double(tab1.get(0).getX()*50,250 + tab1.get(0).getY()*50,tab1.get(1).getX()*50,250 + tab1.get(1).getY()*50));
path2 = new Path2D.Double(new Line2D.Double(tab2.get(0).getX()*50,250 + tab2.get(0).getY()*50,tab2.get(1).getX()*50,250 + tab2.get(1).getY()*50));
//on ajoute les autres points aux paths et on trace la ligne
for (int i=2;i<100;i++) {
path.lineTo(tab1.get(i).getX()*50,250 + tab1.get(i).getY()*50);
path2.lineTo(tab2.get(i).getX()*50,250 + tab2.get(i).getY()*50);
}
//creation de list avec la liste des indexs des points avant l'intersection des courbes
// et creations de inter avac la liste des points d'intersection exacts
intersections();
//il y a list.size intersections (on inclus le premier et le dernier point et on crees list.size path a la place des polygon
for(int i=0;i<list.size()-1;i++) {
//on cree le premier segment
polygones.add(new Path2D.Double(new Line2D.Double(tab1.get(list.get(i)).getX()*50,250 + tab1.get(list.get(i)).getY()*50,tab1.get(list.get(i)+1).getX()*50,250 + tab1.get(list.get(i)+1).getY()*50)));
int j = list.get(i)+2;
//on ajoute les points jusqu'au point avant l(intersection
while(( j<=list.get(i+1)) ){
polygones.get(i).lineTo(tab1.get(j).getX()*50,250 + tab1.get(j).getY()*50);
j=j+1;
}
//si ce n'est pas le dernier point on ajoute l'intersection
if (j != 100) {
polygones.get(i).lineTo(inter.get(i+1).getX()*50,250 + inter.get(i+1).getY()*50);
}
j=j-1;
//on ajoute les point de l'autre courbe dans l'autre sens
while(j>=list.get(i) && j >=0) {
polygones.get(i).lineTo(tab2.get(j).getX()*50,250 + tab2.get(j).getY()*50);
j=j-1;
}
}
}
public static void main(String[] args) {
JFrame f = new JFrame();
f.setSize(650,540);
f.add(new Graph());
f.setVisible(true);
}
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g.create();
//si la liste des polygone est crée on affiche
if (polygones.size()>0) {
for(int i=0;i<list.size()-1;i++) {
//on teste quelle courbe est au dessus pour la couleur(attention repere inversé => ( > = <)
if(tab1.get(list.get(i)+1).getY()<tab2.get(list.get(i)+1).getY())
g2.setColor(Color.pink);
else
g2.setColor(Color.cyan);
//on affiche les polygones
g2.fill(polygones.get(i));
}
}
//on affiche les 2 courbes
g2.setColor(Color.red);
g2.draw(path);
g2.setColor(Color.blue);
g2.draw(path2);
}
//creation d'une classe Points avec des doubles
public class Points {
private double x;
private double y;
public Points(double x, double y) {
this.x = x;
this.y = y;
}
public void setX(double x) {
this.x = x;
}
public double getX() {
return x;
}
public void setY(double y) {
this.y = y;
}
public double getY() {
return y;
}
}
//methode retournanat l'intersection de 2 segments
public Points intersection(Points p1, Points p2,Points p3, Points p4) {
double d = (p1.getX()-p2.getX())*(p3.getY()-p4.getY()) - (p1.getY()-p2.getY())*(p3.getX()-p4.getX());
if (d == 0) return null;
double xi = ((p3.getX()-p4.getX())*(p1.getX()*p2.getY()-p1.getY()*p2.getX())-(p1.getX()-p2.getX())*(p3.getX()*p4.getY()-p3.getY()*p4.getX()))/d;
double yi = ((p3.getY()-p4.getY())*(p1.getX()*p2.getY()-p1.getY()*p2.getX())-(p1.getY()-p2.getY())*(p3.getX()*p4.getY()-p3.getY()*p4.getX()))/d;
Points p = new Points(xi,yi);
if (xi < Math.min(p1.getX(),p2.getX()) || xi > Math.max(p1.getX(),p2.getX())) return null;
if (xi < Math.min(p3.getX(),p4.getX()) || xi > Math.max(p3.getX(),p4.getX())) return null;
return p;
}
//methode remplissant inter et list comme precisé plus haut
public void intersections() {
//je mets le premier point dans inter et l'index 0 dans list
inter.add(tab1.get(0));
list.add(0);
for(int i=1; i<99;i++) {
//si les courbes se croisent on ajoute le point d'intersection à inter et l'index du point precedent à list
if ((tab1.get(i).getY()-tab2.get(i).getY())*(tab1.get(i+1).getY()-tab2.get(i+1).getY())<=0) {
inter.add(intersection(tab1.get(i),tab1.get(i+1),tab2.get(i),tab2.get(i+1)));
list.add(new Integer(i));
}
}
//je mets le dernier point dans inter et l'index 99 dans list
inter.add(tab1.get(99));
list.add(99);
}
} |
Partager