Files
projet-dev/src/Ligne.java

128 lines
2.7 KiB
Java
Raw Normal View History

2026-02-02 11:40:03 +01:00
package linea;
2026-02-02 11:19:18 +01:00
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 {
2026-02-02 11:19:18 +01:00
protected int nbSegments = 400;
protected Segment SegCourant;
protected ArrayList<Segment> listeSegments = new ArrayList<Segment>();
protected double limiteHaut = 50;
protected double limiteBas = 550;
2026-03-04 15:00:29 +01:00
protected Segment dernierSegment;
private double decalageXDernierSegment = 0;
protected double noiseFrequence = 1/50f;
private NoiseGenerator noiseGenerator;
private double xActuel;
2026-02-23 11:29:41 +01:00
public Ligne(NoiseGenerator noiseGenerator) {
2026-02-10 16:24:44 +01:00
double x = 0;
2026-03-04 15:00:29 +01:00
this.noiseGenerator = noiseGenerator;
for (int i = 0; i < nbSegments; i++) {
2026-02-23 11:29:41 +01:00
2026-03-04 15:00:29 +01:00
Segment s = CreerSegment(x);
2026-03-04 15:00:29 +01:00
listeSegments.add(s);
if (i == nbSegments - 1)
{
dernierSegment = s;
xActuel = x + s.xLong;
}
2026-03-04 15:00:29 +01:00
x += s.xLong;
}
}
2026-03-04 15:00:29 +01:00
protected double GetLargeurSegment(){
return (double)ZoneDessin.largeur / nbSegments;
}
2026-03-04 15:00:29 +01:00
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;
2026-02-02 11:19:18 +01:00
}
@Override
public void Afficher(Graphics g) {
2026-02-02 11:19:18 +01:00
Graphics2D g2D = (Graphics2D) g;
g2D.setStroke(new BasicStroke(3.0f));
for (Segment s : listeSegments) {
s.Afficher(g);
if (SegCourant == null) {
2026-03-04 15:00:29 +01:00
if (Cercle.xCercle <= s.x + s.xLong && Cercle.xCercle >= s.x) {
SegCourant = s;
2026-02-10 16:24:44 +01:00
}
} else {
2026-03-04 15:00:29 +01:00
if ((SegCourant.x + SegCourant.xLong) < Cercle.xCercle) {
if (s.x <= Cercle.xCercle && (s.x + s.xLong) >= Cercle.xCercle) {
SegCourant = s;
2026-02-10 16:24:44 +01:00
}
}
}
}
2026-02-02 11:19:18 +01:00
}
@Override
public void Animer() {
for (Segment s : listeSegments) {
s.Animer();
2026-02-10 16:24:44 +01:00
s.x -= 10;
}
2026-03-04 15:00:29 +01:00
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;
}
}
2026-02-02 11:19:18 +01:00
}
2026-02-23 10:07:29 +01:00
public double GetHauteurPointCercle(){
double t = (400 - SegCourant.x) / SegCourant.xLong;
return SegCourant.y + SegCourant.yLong * t;
}
}