generation infinie de ligne

This commit is contained in:
MOISOIU Stefan-Mihai
2026-03-04 15:00:29 +01:00
parent eb39ea8b29
commit 74b214e89c
3 changed files with 77 additions and 26 deletions

View File

@@ -21,14 +21,16 @@ public class Cercle extends ObjetGraphique { // il s'agit plutôt d'arcs de cerc
protected double pas = 0.2; protected double pas = 0.2;
protected double impulsion = 35; protected double impulsion = 35;
public static int xCercle = 400;
public Cercle() { public Cercle() {
x = 400; x = xCercle;
y = 200; y = (double)ZoneDessin.hauteur / 2;
} }
public Cercle(double debutArc, double finArc) { public Cercle(double debutArc, double finArc) {
x = 400; x = xCercle;
y = 200; y = (double)ZoneDessin.hauteur / 2;
debut = debutArc; debut = debutArc;
fin = finArc; fin = finArc;
} }

View File

@@ -9,7 +9,6 @@ import java.util.ArrayList;
public class Ligne extends ObjetGraphique { public class Ligne extends ObjetGraphique {
protected int nbSegments = 400; protected int nbSegments = 400;
protected double xCercle = 400;
protected Segment SegCourant; protected Segment SegCourant;
protected ArrayList<Segment> listeSegments = new ArrayList<Segment>(); protected ArrayList<Segment> listeSegments = new ArrayList<Segment>();
@@ -17,34 +16,61 @@ public class Ligne extends ObjetGraphique {
protected double limiteHaut = 50; protected double limiteHaut = 50;
protected double limiteBas = 550; 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) { public Ligne(NoiseGenerator noiseGenerator) {
double x = 0; double x = 0;
double y = 200;
double dx, dy;
Segment s; this.noiseGenerator = noiseGenerator;
for (int i = 0; i < nbSegments; i++) { for (int i = 0; i < nbSegments; i++) {
dx = 10;
dy = noiseGenerator.noise(x / 15) * 8; Segment s = CreerSegment(x);
if (y + dy > limiteBas) {
dy = limiteBas - y;
}
if (y + dy < limiteHaut) {
dy = limiteHaut - y;
}
s = new Segment(x, y, dx, dy);
s.setCouleur(new Color(0.2f, 0.2f, 0.2f));
listeSegments.add(s); listeSegments.add(s);
x += dx; if (i == nbSegments - 1)
y += dy; {
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 @Override
public void Afficher(Graphics g) { public void Afficher(Graphics g) {
Graphics2D g2D = (Graphics2D) g; Graphics2D g2D = (Graphics2D) g;
@@ -53,12 +79,12 @@ public class Ligne extends ObjetGraphique {
for (Segment s : listeSegments) { for (Segment s : listeSegments) {
s.Afficher(g); s.Afficher(g);
if (SegCourant == null) { if (SegCourant == null) {
if (xCercle <= s.x + s.xLong && xCercle >= s.x) { if (Cercle.xCercle <= s.x + s.xLong && Cercle.xCercle >= s.x) {
SegCourant = s; SegCourant = s;
} }
} else { } else {
if ((SegCourant.x + SegCourant.xLong) < xCercle) { if ((SegCourant.x + SegCourant.xLong) < Cercle.xCercle) {
if (s.x <= xCercle && (s.x + s.xLong) >= xCercle) { if (s.x <= Cercle.xCercle && (s.x + s.xLong) >= Cercle.xCercle) {
SegCourant = s; SegCourant = s;
} }
} }
@@ -72,6 +98,26 @@ public class Ligne extends ObjetGraphique {
s.Animer(); s.Animer();
s.x -= 10; 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(){ public double GetHauteurPointCercle(){

View File

@@ -18,9 +18,12 @@ public class ZoneDessin extends JPanel {
protected boolean estArrete = false; protected boolean estArrete = false;
public static int largeur = 800;
public static int hauteur = 600;
public ZoneDessin() { public ZoneDessin() {
setLayout(new BorderLayout()); setLayout(new BorderLayout());
setPreferredSize(new Dimension(800, 600)); setPreferredSize(new Dimension(largeur, hauteur));
setBackground(new Color(0, 73, 220)); setBackground(new Color(0, 73, 220));
} }