Jump to content
Unity Insider Forum

Schnittpunkte einer Geraden mit einem Kreis


Recommended Posts

protected static List<Intersection> LineCircleIntersection(Line l, Circle k)
{
  float a = l.p1.y - l.p2.y;
  float b = l.p2.x - l.p1.x;
  float c = l.p2.x * l.p1.y - l.p1.x * l.p2.y;
  float d = c - a * k.origin.x - b * k.origin.y;

  float t = k.radius*k.radius * (a*a + b*b);

  var list = new List<Intersection>();
  if (t < d*d) return list;

  float sq = Mathf.Sqrt(k.radius*k.radius * (a*a + b*b) - (d*d));

  float x1 = k.origin.x - (a*d + b * sq) / (a*a+b*b);
  float y1 = k.origin.y + (b*d + a * sq) / (a*a+b*b);

  float x2 = k.origin.x - (a*d - b * sq) / (a*a+b*b);
  float y2 = k.origin.y + (b*d - a * sq) / (a*a+b*b);


  list.Add(new Intersection(new Vector3(x1, y1, 0), l, k));
  if (t > d*d)
    list.Add(new Intersection(new Vector3(x2, y2, 0), l, k));
  return list;
}

Hallo,

ich möchte die Schnittpunkte einer Linie mit einem Kreis berechnen. Leider liegen die Schnittpunkte meines Algorithmus etwas daneben.

Wenn der Kreis-Mittelpunkt auf der Linie liegt, funktioniert es.

Formel ist von Wikipedia adaptiert: https://de.wikipedia.org/wiki/Schnittpunkt#Schnittpunkte_einer_Geraden_mit_einem_Kreis

Linie ist definiert durch 2 Vektoren. Kreis durch Mitte und Radius.

Ich finde den Fehler einfach nicht :(

mfg TWS

Link zu diesem Kommentar
Auf anderen Seiten teilen

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Gast
Auf dieses Thema antworten...

×   Du hast formatierten Text eingefügt.   Formatierung jetzt entfernen

  Only 75 emoji are allowed.

×   Dein Link wurde automatisch eingebettet.   Einbetten rückgängig machen und als Link darstellen

×   Dein vorheriger Inhalt wurde wiederhergestellt.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Lädt...
×
×
  • Neu erstellen...