128 lines
2.7 KiB
Java
128 lines
2.7 KiB
Java
package linea;
|
|
|
|
import java.awt.BasicStroke;
|
|
import java.awt.Color;
|
|
import java.awt.Graphics;
|
|
import java.awt.Graphics2D;
|
|
import java.util.ArrayList;
|
|
|
|
public class Ligne extends ObjetGraphique {
|
|
|
|
protected int nbSegments = 80;
|
|
protected Segment SegCourant;
|
|
|
|
protected ArrayList<Segment> listeSegments = new ArrayList<Segment>();
|
|
|
|
protected double limiteHaut = 50;
|
|
protected double limiteBas = 550;
|
|
|
|
protected Segment dernierSegment;
|
|
private double decalageXDernierSegment = 0;
|
|
|
|
protected double noiseFrequence = 1/50f;
|
|
|
|
private NoiseGenerator noiseGenerator;
|
|
|
|
private double xActuel;
|
|
|
|
public Ligne(NoiseGenerator noiseGenerator) {
|
|
double x = 0;
|
|
|
|
this.noiseGenerator = noiseGenerator;
|
|
for (int i = 0; i < nbSegments; i++) {
|
|
|
|
Segment s = CreerSegment(x);
|
|
|
|
listeSegments.add(s);
|
|
|
|
if (i == nbSegments - 1)
|
|
{
|
|
dernierSegment = s;
|
|
xActuel = x + s.xLong;
|
|
}
|
|
|
|
x += s.xLong;
|
|
}
|
|
}
|
|
|
|
protected double GetLargeurSegment(){
|
|
return (double)ZoneDessin.largeur / nbSegments;
|
|
}
|
|
|
|
protected Segment CreerSegment(double x){
|
|
double dx = GetLargeurSegment();
|
|
double y = CalculerY(x);
|
|
double dy = CalculerY(x + dx) - y;
|
|
|
|
Segment s = new Segment(x, y, dx, dy);
|
|
|
|
s.setCouleur(new Color(0.2f, 0.2f, 0.2f));
|
|
|
|
return s;
|
|
}
|
|
|
|
protected double CalculerY(double x){
|
|
|
|
double hauteur = limiteBas - limiteHaut;
|
|
|
|
double bruit = noiseGenerator.noise(x * noiseFrequence);
|
|
bruit = (bruit + 1) / 2;
|
|
|
|
return limiteBas - bruit * hauteur;
|
|
}
|
|
|
|
@Override
|
|
public void Afficher(Graphics g) {
|
|
Graphics2D g2D = (Graphics2D) g;
|
|
g2D.setStroke(new BasicStroke(3.0f));
|
|
|
|
for (Segment s : listeSegments) {
|
|
s.Afficher(g);
|
|
if (SegCourant == null) {
|
|
if (Cercle.xCercle <= s.x + s.xLong && Cercle.xCercle >= s.x) {
|
|
SegCourant = s;
|
|
}
|
|
} else {
|
|
if ((SegCourant.x + SegCourant.xLong) < Cercle.xCercle) {
|
|
if (s.x <= Cercle.xCercle && (s.x + s.xLong) >= Cercle.xCercle) {
|
|
SegCourant = s;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void Animer() {
|
|
for (Segment s : listeSegments) {
|
|
s.Animer();
|
|
s.x -= 10;
|
|
}
|
|
|
|
UpdateSegments();
|
|
}
|
|
|
|
private void UpdateSegments(){
|
|
|
|
if (dernierSegment == null) throw new RuntimeException("dernierSegment n'existe pas");
|
|
|
|
for (int i = 0; i < listeSegments.size(); i++) {
|
|
|
|
Segment s = listeSegments.get(i);
|
|
if (s.x + s.xLong < 0) {
|
|
Segment nouveauSegment = CreerSegment(xActuel);
|
|
nouveauSegment.x = dernierSegment.x + dernierSegment.xLong;
|
|
listeSegments.set(i, nouveauSegment);
|
|
|
|
xActuel += GetLargeurSegment();
|
|
dernierSegment = nouveauSegment;
|
|
}
|
|
}
|
|
}
|
|
|
|
public double GetHauteurPointCercle(){
|
|
double t = (400 - SegCourant.x) / SegCourant.xLong;
|
|
|
|
return SegCourant.y + SegCourant.yLong * t;
|
|
}
|
|
} |