La meilleure façon de répondre était, pour moi, d'expérimenter avec ce sujet, car il m'intéresse.
J'ai utilisé Processing pour la partie visuelle (et son PVector, mais il est trivial dans son utilisation ici). Fondamentalement, c'est du code Java. J'ai utilisé la formule de l'article Wikipedia's Line-line intersection.
int MARGIN = 10;
int POINT_SIZE = 7;
// Definition of the bouding box
float xMin, xMax;
float yMin, yMax;
// The two points inside the bounding box
// A PVector is just a pair of x and y coordinates
PVector pointA = new PVector();
PVector pointB = new PVector();
// The intersection points
PVector[] pointsI = new PVector[2];
void setup()
{
size(800, 800);
MakeBB();
SetPoints();
FindIntersections();
}
void draw()
{
background(#DDFFFF);
stroke(#FFFF00);
fill(#8000FF);
rect(xMin, yMin, xMax - xMin, yMax - yMin);
noStroke();
fill(#FF8000);
ellipse(pointA.x, pointA.y, POINT_SIZE, POINT_SIZE);
fill(#FF8000);
ellipse(pointB.x, pointB.y, POINT_SIZE, POINT_SIZE);
stroke(#FFFF00);
strokeWeight(5);
line(pointA.x, pointA.y, pointB.x, pointB.y);
noStroke();
fill(#FF0000);
ellipse(pointsI[0].x, pointsI[0].y, POINT_SIZE * 2, POINT_SIZE * 2);
fill(#FF0000);
ellipse(pointsI[1].x, pointsI[1].y, POINT_SIZE * 2, POINT_SIZE * 2);
stroke(#FF8000);
strokeWeight(1);
line(pointsI[0].x, pointsI[0].y, pointsI[1].x, pointsI[1].y);
}
void keyPressed()
{
MakeBB();
SetPoints();
FindIntersections();
}
// Make bounding box
void MakeBB()
{
xMin = (int) random(MARGIN, width/2);
xMax = (int) random(width/2, width - MARGIN);
yMin = (int) random(MARGIN, height/2);
yMax = (int) random(height/2, height - MARGIN);
}
void SetPoints()
{
pointA.x = (int) random(xMin, xMax);
pointA.y = (int) random(yMin, yMax);
pointB.x = (int) random(xMin, xMax);
pointB.y = (int) random(yMin, yMax);
}
void FindIntersections()
{
// The corners of the BB
PVector pTL = new PVector(xMin, yMin);
PVector pBL = new PVector(xMin, yMax);
PVector pTR = new PVector(xMax, yMin);
PVector pBR = new PVector(xMax, yMax);
// The sides of the BB
PVector pT = IntersectLines(pTL, pTR);
PVector pB = IntersectLines(pBL, pBR);
PVector pL = IntersectLines(pTL, pBL);
PVector pR = IntersectLines(pTR, pBR);
int i = 0;
// Eliminates the intersection points out of the segments
if (pT != null && pT.x >= xMin && pT.x <= xMax) pointsI[i++] = pT;
if (pB != null && pB.x >= xMin && pB.x <= xMax) pointsI[i++] = pB;
if (pL != null && pL.y >= yMin && pL.y <= yMax) pointsI[i++] = pL;
if (pR != null && pR.y >= yMin && pR.y <= yMax) pointsI[i++] = pR;
}
// Compute intersection of the line made of pointA and pointB
// with the given line defined by two points
PVector IntersectLines(PVector p1, PVector p2)
{
PVector pRes = new PVector();
float v1 = pointA.x * pointB.y - pointA.y * pointB.x;
float v2 = p1.x * p2.y - p1.y * p2.x;
float d = (pointA.x - pointB.x) * (p1.y - p2.y) -
(pointA.y - pointB.y) * (p1.x - p2.x);
if (d == 0)
{
println("Ouch!");
return null;
}
pRes.x = (v1 * (p1.x - p2.x) - (pointA.x - pointB.x) * v2)/d;
pRes.y = (v1 * (p1.y - p2.y) - (pointA.y - pointB.y) * v2)/d;
return pRes;
}
C'est un hack rapide, pas optimisé et tout. Mais ça marche ... :-)
Je le fais déjà et ça ne marche pas pour moi, c'est pourquoi j'ai demandé. –
Mais je le faisais mal, et maintenant c'est corrigé. Apparemment, je ne me souviens pas de l'algèbre de 7e année –