I
Identity80
Guest
Hi all !
I need to know, understand , learn ,how can i draw in 3d (lines only,not more).
l search the simpliest code ever ,with only one mouse event.
I find one code, what working with dots. I "complete" with this code:
(
Dim koord1 As New Dot
Dim koord2 As New Dot
For i = 0 To koordList.ToArray.Count - 2
koord1 = koordList(i)
koord2 = koordList(i + 1)
e.Graphics.DrawLine(Pens.Red, CSng(koord1.point.X), CSng(koord1.point.Y), CSng(koord2.point.X), CSng(koord2.point.Y))
Next
)
Now i see the lines, i can scale ,but only the dots rotate .
Thank you ,Regards Zoli.
The code:
Public Class Form1
Const twoPI As Double = Math.PI * 2
Private Structure Vertex3
Public X As Single
Public Y As Single
Public Z As Single
End Structure
Private Structure Dot
Public point As Vertex3
Public koordColor As Color
Public Sub Draw(g As Graphics)
Using dotBrush As New SolidBrush(koordColor)
g.FillEllipse(dotBrush, point.X, point.Y, 2, 2)
End Using
End Sub
End Structure
Private rand As New Random
Private koordList As New List(Of Dot)
Private koordRender() As Dot
Private AngX As Double
Private AngY As Double
Private lastAngX As Double
Private lastAngY As Double
Private deltaAngX As Double
Private deltaAngY As Double
Private ScaleFactor As Single = 1.0F
Private zeroPoint As PointF
Private UseBodyAxis As Boolean = True
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
DoubleBuffered = True
Dim koord As Dot
'create some 3D points to draw
For i = 0 To 8
koord.koordColor = Color.Yellow
koord.point.X = rand.Next(40, 100)
koord.point.Y = rand.Next(-100, 100)
koord.point.Z = rand.Next(-100, 100)
koordList.Add(koord)
Next
koordRender = koordList.ToArray
zeroPoint = New PointF(ClientSize.Width / 2, ClientSize.Height / 2)
End Sub
Private Sub Form1_MouseMove(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
Static lastPos As Point
Dim CosAngX As Double
Dim SinAngX As Double
Dim CosAngZ As Double
Dim SinAngZ As Double
Dim CosAngY As Double
Dim SinAngY As Double
Dim xi, yi, zi As Double
If e.Button = MouseButtons.Left Then
If My.Computer.Keyboard.ShiftKeyDown Then 'Designating screen center
zeroPoint = e.Location
Invalidate()
Else 'Modifying rotation angles
AngX += (e.X - lastPos.X) / 100.0F
AngY -= (e.Y - lastPos.Y) / 100.0F
deltaAngX = AngX - lastAngX
deltaAngY = AngY - lastAngY
If AngX > Math.PI Then 'limit to +/- 180 degrees
AngX -= twoPI
ElseIf AngX < -Math.PI Then
AngX += twoPI
End If
If AngY > Math.PI Then
AngY -= twoPI
ElseIf AngY < -Math.PI Then
AngY += twoPI
End If
lastAngX = AngX
lastAngY = AngY
CosAngX = Math.Cos(AngX) 'use current Angles and reference original points
SinAngX = Math.Sin(AngX)
CosAngZ = 1 'we're not rotating around Z
SinAngZ = 0 'so use 0 degree values
CosAngY = Math.Cos(AngY)
SinAngY = Math.Sin(AngY)
Dim idx As Integer
For Each koord As Dot In koordList
xi = CosAngX * koord.point.X - SinAngX * koord.point.Y
yi = SinAngX * koord.point.X + CosAngX * koord.point.Y
With koordRender(idx).point
.X = CSng(CosAngZ * xi + SinAngZ * koord.point.Z)
zi = CosAngZ * koord.point.Z - SinAngZ * xi
.Y = CSng(CosAngY * yi - SinAngY * zi)
.Z = CSng(SinAngY * yi + CosAngY * zi)
End With
idx += 1
Next
End If
Invalidate()
ElseIf e.Button = MouseButtons.Right Then
ScaleFactor *= 1 + (e.X - lastPos.X) * 0.001F
If ScaleFactor < 0.1F Then ScaleFactor = 0.1F
If ScaleFactor > 10.0F Then ScaleFactor = 10.0F
Invalidate()
End If
lastPos = e.Location
End Sub
Private Sub Form1_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
e.Graphics.TranslateTransform(zeroPoint.X, zeroPoint.Y)
e.Graphics.ScaleTransform(ScaleFactor, ScaleFactor)
For Each koord As Dot In koordRender
koord.Draw(e.Graphics)
Dim koord1 As New Dot
Dim koord2 As New Dot
For i = 0 To koordList.ToArray.Count - 2
koord1 = koordList(i)
koord2 = koordList(i + 1)
e.Graphics.DrawLine(Pens.Red, CSng(koord1.point.X), CSng(koord1.point.Y), CSng(koord2.point.X), CSng(koord2.point.Y))
Next
Next
End Sub
End Class
Continue reading...
I need to know, understand , learn ,how can i draw in 3d (lines only,not more).
l search the simpliest code ever ,with only one mouse event.
I find one code, what working with dots. I "complete" with this code:
(
Dim koord1 As New Dot
Dim koord2 As New Dot
For i = 0 To koordList.ToArray.Count - 2
koord1 = koordList(i)
koord2 = koordList(i + 1)
e.Graphics.DrawLine(Pens.Red, CSng(koord1.point.X), CSng(koord1.point.Y), CSng(koord2.point.X), CSng(koord2.point.Y))
Next
)
Now i see the lines, i can scale ,but only the dots rotate .
Thank you ,Regards Zoli.
The code:
Public Class Form1
Const twoPI As Double = Math.PI * 2
Private Structure Vertex3
Public X As Single
Public Y As Single
Public Z As Single
End Structure
Private Structure Dot
Public point As Vertex3
Public koordColor As Color
Public Sub Draw(g As Graphics)
Using dotBrush As New SolidBrush(koordColor)
g.FillEllipse(dotBrush, point.X, point.Y, 2, 2)
End Using
End Sub
End Structure
Private rand As New Random
Private koordList As New List(Of Dot)
Private koordRender() As Dot
Private AngX As Double
Private AngY As Double
Private lastAngX As Double
Private lastAngY As Double
Private deltaAngX As Double
Private deltaAngY As Double
Private ScaleFactor As Single = 1.0F
Private zeroPoint As PointF
Private UseBodyAxis As Boolean = True
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
DoubleBuffered = True
Dim koord As Dot
'create some 3D points to draw
For i = 0 To 8
koord.koordColor = Color.Yellow
koord.point.X = rand.Next(40, 100)
koord.point.Y = rand.Next(-100, 100)
koord.point.Z = rand.Next(-100, 100)
koordList.Add(koord)
Next
koordRender = koordList.ToArray
zeroPoint = New PointF(ClientSize.Width / 2, ClientSize.Height / 2)
End Sub
Private Sub Form1_MouseMove(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
Static lastPos As Point
Dim CosAngX As Double
Dim SinAngX As Double
Dim CosAngZ As Double
Dim SinAngZ As Double
Dim CosAngY As Double
Dim SinAngY As Double
Dim xi, yi, zi As Double
If e.Button = MouseButtons.Left Then
If My.Computer.Keyboard.ShiftKeyDown Then 'Designating screen center
zeroPoint = e.Location
Invalidate()
Else 'Modifying rotation angles
AngX += (e.X - lastPos.X) / 100.0F
AngY -= (e.Y - lastPos.Y) / 100.0F
deltaAngX = AngX - lastAngX
deltaAngY = AngY - lastAngY
If AngX > Math.PI Then 'limit to +/- 180 degrees
AngX -= twoPI
ElseIf AngX < -Math.PI Then
AngX += twoPI
End If
If AngY > Math.PI Then
AngY -= twoPI
ElseIf AngY < -Math.PI Then
AngY += twoPI
End If
lastAngX = AngX
lastAngY = AngY
CosAngX = Math.Cos(AngX) 'use current Angles and reference original points
SinAngX = Math.Sin(AngX)
CosAngZ = 1 'we're not rotating around Z
SinAngZ = 0 'so use 0 degree values
CosAngY = Math.Cos(AngY)
SinAngY = Math.Sin(AngY)
Dim idx As Integer
For Each koord As Dot In koordList
xi = CosAngX * koord.point.X - SinAngX * koord.point.Y
yi = SinAngX * koord.point.X + CosAngX * koord.point.Y
With koordRender(idx).point
.X = CSng(CosAngZ * xi + SinAngZ * koord.point.Z)
zi = CosAngZ * koord.point.Z - SinAngZ * xi
.Y = CSng(CosAngY * yi - SinAngY * zi)
.Z = CSng(SinAngY * yi + CosAngY * zi)
End With
idx += 1
Next
End If
Invalidate()
ElseIf e.Button = MouseButtons.Right Then
ScaleFactor *= 1 + (e.X - lastPos.X) * 0.001F
If ScaleFactor < 0.1F Then ScaleFactor = 0.1F
If ScaleFactor > 10.0F Then ScaleFactor = 10.0F
Invalidate()
End If
lastPos = e.Location
End Sub
Private Sub Form1_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
e.Graphics.TranslateTransform(zeroPoint.X, zeroPoint.Y)
e.Graphics.ScaleTransform(ScaleFactor, ScaleFactor)
For Each koord As Dot In koordRender
koord.Draw(e.Graphics)
Dim koord1 As New Dot
Dim koord2 As New Dot
For i = 0 To koordList.ToArray.Count - 2
koord1 = koordList(i)
koord2 = koordList(i + 1)
e.Graphics.DrawLine(Pens.Red, CSng(koord1.point.X), CSng(koord1.point.Y), CSng(koord2.point.X), CSng(koord2.point.Y))
Next
Next
End Sub
End Class
Continue reading...