Hallo!
Ohne genauere Angaben, was Du erreichen willst, ist es kaum
möglich, Dir gezielte Hinweise zu geben.
Gibt es z.B. ein festes Koordinatensystem mit vorgegebenem Nullpunkt,
in dem das Dreieck positioniert werden soll?
Soll die Größe der Darstellung auf dem Bildschirm der "Originalgröße"
entsprechen? Oder können Skalen an die Größenverhältnissen der
Zeichnungselemente angepaßt werden?
Vielleicht hilft dieses Beispiel weiter.
Maßstab: Pixel
Die Bitmap-Größe wird an die Position des Dreiecks angepaßt.
Picturebox-Sizeimage steht auf Zoom - automatische Anpassung.
Schau Dir auch mal die Transform-Methode des Graphics-Objects an.
Option Strict On : Option Explicit On
Public Class frmTriangle
Dim pn As New Panel With _
{.Parent = Me, .Top = 50, .Width = 400, .Height = 300}
Dim pb As New PictureBox With _
{.Parent = pn, .SizeMode = PictureBoxSizeMode.Zoom, .Dock = _
DockStyle.Fill}
Dim tba As New TextBox With _
{.Parent = Me, .Top = 0, .Width = 80}
Dim tbb As New TextBox With _
{.Parent = Me, .Top = 0, .Left = 100, .Width = 80}
Dim tbc As New TextBox With _
{.Parent = Me, .Top = 0, .Left = 200, .Width = 80}
Dim WithEvents btnDraw As New Button With _
{.Parent = Me, .Left = 300, .Text = "Draw"}
Dim pos1, pos2, pos3 As Point
Private Sub frmTriangle_Load(sender As System.Object, _
e As System.EventArgs) Handles MyBase.Load
Me.Width = 400 : Me.Height = 400
Me.FormBorderStyle = Windows.Forms.FormBorderStyle.Fixed3D
Me.Text = "Seitenlängen a,b,c eingeben -- a + b => c"
tba.Text = CStr(100) : tbb.Text = CStr(120) : tbc.Text = CStr(150)
End Sub
Private Sub btnDraw_Click(sender As Object, _
e As System.EventArgs) Handles btnDraw.Click
Dim a, b, c As Integer, ok As Boolean = True
'Eingaben checken
If Not Integer.TryParse(tba.Text, a) Then Exit Sub
If Not Integer.TryParse(tbb.Text, b) Then Exit Sub
If Not Integer.TryParse(tbc.Text, c) Then Exit Sub
'Kosinussatz anwenden
Dim cosval As Double = (a * a - (b * b + c * c)) / (-2 * b * c)
Dim alpha As Double = Math.Acos(cosval)
'Definieren Seitenlängen ein Dreieck?
If Double.IsNaN(alpha) Then
MsgBox("Ungeeignete Eingabe", MsgBoxStyle.Exclamation) : Exit Sub
End If
'Zwei Punkte festlegen
pos1 = New Point(100, 100)
pos2 = New Point(100 + CInt(b), 100)
'Position des 3. Punktes gemäß obigem Winkel bestimmen
pos3 = New Point(pos1.X + CInt(Math.Cos(alpha) * c), _
pos1.Y + CInt(Math.Sin(alpha) * c))
Dim xmax As Integer = CInt(Math.Max(pos2.X, pos3.X) + 50)
Dim ymax As Integer = CInt(Math.Max(pos2.Y, pos3.Y) + 50)
'Überprüfung der Punkte-Position:
'Euklidische Distanz zum dritten Punkt berechnen
Dim diff1 As Integer = _
CInt(Math.Sqrt((pos1.X - pos3.X) ^ 2 + (pos1.Y - pos3.Y) ^ 2))
If Math.Abs(diff1 - c) > 2 Then ok = False
Dim diff2 As Integer = _
CInt(Math.Sqrt((pos2.X - pos3.X) ^ 2 + (pos2.Y - pos3.Y) ^ 2))
If Math.Abs(diff2 - a) > 2 Then ok = False
If Not ok Then
MsgBox("Berechnungsfehler", MsgBoxStyle.Exclamation)
Else
Using bmp As New Bitmap(xmax, ymax), _
gr As Graphics = Graphics.FromImage(bmp)
gr.Clear(Color.White)
'Dreieck Zeichnen
gr.FillPolygon _
(New SolidBrush(Color.Red), New Point() {pos1, pos2, pos3})
gr.DrawPolygon _
(New Pen(Color.Blue, 3), New Point() {pos1, pos2, pos3})
'Dreieck anzeigen
pb.Image = CType(bmp.Clone, Image)
End Using
End If
End Sub
End Class
Beitrag wurde zuletzt am 09.12.12 um 17:24:17 editiert. |