Autor Beitrag
Coder
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 1383
Erhaltene Danke: 1

WinXP
D2005 PE
BeitragVerfasst: Do 18.01.07 22:23 
Hi
Hab ein kleines Problemchen.
Ich hab von zwei Geraden Gleichungen je m und b.
Wie kann ich nun den Schnittpunkt dieser beiden Geraden Gleichungen berechnen?
Ich weis ich muss gleichsetzen und mit Zahlen bekomm ich das auch hin.
Für Delphi brauch ich aber eine schön kompakte Formel.

Soweit bin ich.
Die Zahlen sind nur Indexe für die beiden Gleichungen.

y1 = m1 * x1 + b1
y2 = m2 * x2 + b2

m1 * x1 + b1 = m2 * x2 + b2

0 = (m2 * x2 + b2) : (m1 * x1 + b1)

Aber wie bekomm ich jetzt x auf eine Seite?

Vielleicht nen bisschen Doof die Frage aber was ich auch versuche, es geht nicht.

Einen Mathe-Parser will ich dafür nicht verwenden weil ich ja nur die eine Formel brauche.

MfG


Moderiert von user profile iconChristian S.: Topic aus Sonstiges (Delphi) verschoben am Fr 19.01.2007 um 11:54 (wohl eher 'ne Mathe-Frage)
LLCoolDave
ontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic starofftopic star
Beiträge: 212

Win XP
Delphi 2005
BeitragVerfasst: Do 18.01.07 22:28 
Müsste eigentlich x = (b2-b1)/(m1-m2) sein. Deine Formel hat übrigens nen Fehler, da kommt links sicher nicht 0 sondern 1 raus ;)
Jann1k
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 866
Erhaltene Danke: 43

Win 7
TurboDelphi, Visual Studio 2010
BeitragVerfasst: Do 18.01.07 22:32 
yep, mein Vorredner hat in allen Punkten recht...
IngoD7
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 629


D7
BeitragVerfasst: Fr 19.01.07 11:48 
user profile iconCoder hat folgendes geschrieben:


m1 * x1 + b1 = m2 * x2 + b2

Aber wie bekomm ich jetzt x auf eine Seite?

ausblenden Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
m1 * x + b1      = m2 * x + b2         / - b1
m1 * x           = m2 * x + b2 - b1    / - (m2 * x2)
m1 * x - m2 * x  = b2 - b1             / x ausklammern
x * (m1 - m2)    = b2 - b1             / : (m1 - m2)

    b2 - b1
x = -------
    m1 - m2
Coder Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 1383
Erhaltene Danke: 1

WinXP
D2005 PE
BeitragVerfasst: So 21.01.07 03:58 
Danke!
Hab das jetzt mal in einem Testprogramm umgesetzt.
Da eine Linie auch mal senkrecht sein kann, musste ich ein paar if-Anweisungen einbauen.
Funktioniert Prima.

Wer will kann es sich ja mal angucken und mir Tipps geben, was ich noch verbessern oder verkürzen könnte.
Wichtig ist eigentlich nur die function CrossLine.
ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
var
  Form1: TForm1;
  aPoints: Array [0..4] of TPoint;
  mDown: Boolean;
  mStart: TPoint;

function CrossLine(MouseStart, MouseEnd, LineStart, LineEnd: TPoint): TPoint;
var
  mMouse, bMouse,
  mLine, bLine: Double;
begin
  Result.X := -1;
  Result.Y := -1;
  
  if MouseStart.X - MouseEnd.X = 0 then
  begin
    if LineStart.X - LineEnd.X <> 0 then
    begin
      mLine := (LineStart.Y - LineEnd.Y) / (LineStart.X - LineEnd.X);
      bLine := ((mLine * LineStart.X) - LineStart.Y) * -1;
      Result.X := MouseStart.X;
      Result.Y := Round((mLine * Result.X) + bLine);
    end;
  end else
  begin
    if MouseStart.Y - MouseEnd.Y = 0 then
    begin
      if LineStart.X - LineEnd.X = 0 then
      begin
        Result.X := LineStart.X;
        Result.Y := MouseStart.Y;
      end else
      begin
        if LineStart.Y - LineEnd.Y <> 0 then
        begin
          mLine := (LineStart.Y - LineEnd.Y) / (LineStart.X - LineEnd.X);
          bLine := ((mLine * LineStart.X) - LineStart.Y) * -1;
          Result.Y := MouseStart.Y;
          Result.X := Round((Result.Y - bLine) / mLine);
        end;
      end;
    end else
    begin
      if LineStart.X - LineEnd.X = 0 then
      begin
        mMouse := (MouseStart.Y - MouseEnd.Y) / (MouseStart.X - MouseEnd.X);
        bMouse := ((mMouse * MouseStart.X) - MouseStart.Y) * -1;
        Result.X := LineStart.X;
        Result.Y := Round((mMouse * Result.X) + bMouse);
      end else
      begin
        mMouse := (MouseStart.Y - MouseEnd.Y) / (MouseStart.X - MouseEnd.X);
        bMouse := ((mMouse * MouseStart.X) - MouseStart.Y) * -1;
        mLine := (LineStart.Y - LineEnd.Y) / (LineStart.X - LineEnd.X);
        bLine := ((mLine * LineStart.X) - LineStart.Y) * -1;
        if mMouse - mLine = 0 then
          Result.X := Round(bLine - bMouse)
        else
          Result.X := Round((bLine - bMouse) / (mMouse - mLine));
        Result.Y := Round((mLine * Result.X) + bLine);
      end;
    end;
  end;

  if not (
    (((Result.X >= LineStart.X) and
      (Result.X <= LineEnd.X)) or
     ((Result.X >= LineEnd.X) and
      (Result.X <= LineStart.X))) and
    (((Result.Y >= LineStart.Y) and
      (Result.Y <= LineEnd.Y)) or
     ((Result.Y >= LineEnd.Y) and
      (Result.Y <= LineStart.Y)))) then
  begin
    Result.X := -1;
    Result.Y := -1;
  end;
end;

procedure TForm1.PaintBox1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  mDown := true;
  mStart := Point(X,Y);
end;

procedure TForm1.PaintBox1MouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
var
  i: Integer;
  aPoint: TPoint;
begin
  mDown := false;
  PaintBox1.Refresh;
  FormPaint(nil);

  for i := 0 to Length(aPoints)-2 do
  begin
    aPoint := CrossLine(mStart, Point(X,Y), aPoints[i], aPoints[i+1]);
    if (aPoint.Y > -1) and
       (aPoint.Y > -1) then
      PaintBox1.Canvas.Pixels[aPoint.X, aPoint.Y] := clBlack;
  end;
end;

procedure TForm1.FormPaint(Sender: TObject);
begin
  aPoints[0] := Point(100,100);
  aPoints[1] := Point(200,150);
  aPoints[2] := Point(250,250);
  aPoints[3] := Point(100,250);
  aPoints[4] := Point(100,100);
  PaintBox1.Canvas.Pen.Color := clWhite;
  PaintBox1.Canvas.Polyline(aPoints);
end;


MfG,
Coder
Einloggen, um Attachments anzusehen!