Bonjour,
Je créer des lignes avec des coordonnée aléatoire et j’aimerai savoir quant un ligne touche une autre ligne .
Mon objectif et de créer plusieurs lignes de façon aléatoire mais qui ne se touche pas.
Dernière modification par gregoire0 (2021-06-26 19:37:38)
Hors ligne
J'ai trouvé ce code qui est de bonne qualité sur le forum de Processing.
/**
* Line Intersections (v1.0)
* Mod: GoToLoop (2019/Jul/27)
*
* https://www.YouTube.com/watch?v=4bIsntTiKfM
* https://GitHub.com/bit101/CodingMath/blob/master/episode32/main.js
*
* https://Discourse.Processing.org/t/correct-practice-line-intersect/13017/5
* https://www.OpenProcessing.org/sketch/740740
*/
static final color BG = 0350;
static final int DIAM = 20;
final float[][] dots = {
{ 100, 100 }, { 400, 400 }, // line A coords
{ 500, 50 }, { 80, 500 } // line B coords
};
void setup() {
size(600, 600);
noLoop();
noFill();
for (final float[] coord : dots) println(coord);
print("\n");
}
void draw() {
background(BG);
line(dots[0][X], dots[0][Y], dots[1][X], dots[1][Y]); // line A
line(dots[2][X], dots[2][Y], dots[3][X], dots[3][Y]); // line B
final float[] inter = lineIntersect(dots[0], dots[1], dots[2], dots[3]);
println(inter);
ellipse(inter[X], inter[Y], DIAM, DIAM); // intersection mark
}
static final float[] lineIntersect(
final float[] p0, final float[] p1, // line A coords
final float[] p2, final float[] p3) // line B coords
{
final float
a1 = p1[Y] - p0[Y],
b1 = p0[X] - p1[X],
c1 = a1*p0[X] + b1*p0[Y],
a2 = p3[Y] - p2[Y],
b2 = p2[X] - p3[X],
c2 = a2*p2[X] + b2*p2[Y],
d = a1*b2 - a2*b1;
return new float[] { (b2*c1 - b1*c2) / d, (a1*c2 - a2*c1) / d };
}Tu peux l'adapter selon tes besoins. @+
Hors ligne
Pages: 1