Results 1 to 9 of 9

Thread: [RESOLVED] Draw 3d object on 2d surface

  1. #1

    Thread Starter
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,760

    Resolved [RESOLVED] Draw 3d object on 2d surface

    I'm trying to use GDI+ to draw a 3d line on a control. Currently this is what I'm trying:
    Code:
    Option Strict On
    Option Explicit On
    Public Class Line3d
        Inherits System.Windows.Forms.Control
    
        Public Property EndPoint As Point3d
        Public Property LineColor As System.Drawing.Color
        Public Property StartPoint As Point3d
    
        <System.ComponentModel.Browsable(False)> _
        Public Overloads Property Location As System.Drawing.Point
    
        <System.ComponentModel.Browsable(False)> _
        Public Overloads Property Size As System.Drawing.Size
    
        Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
            MyBase.OnPaint(e)
    
            Using linePen As System.Drawing.Pen = New System.Drawing.Pen(Me.LineColor, 1)
                Dim start2d, end2d As New System.Drawing.Point
    
                start2d.X = If(Me.StartPoint.Z = 0, Me.StartPoint.X, Me.StartPoint.X \ Me.StartPoint.Z)
                start2d.Y = If(Me.StartPoint.Z = 0, Me.StartPoint.Y, Me.StartPoint.Y \ Me.StartPoint.Z)
                end2d.X = If(Me.StartPoint.Z = 0, Me.StartPoint.X, Me.StartPoint.X \ Me.StartPoint.Z)
                end2d.Y = If(Me.StartPoint.Z = 0, Me.StartPoint.Y, Me.StartPoint.Y \ Me.StartPoint.Z)
    
                Me.Size = New System.Drawing.Size(If(start2d.X > end2d.X, start2d.X, end2d.X), If(start2d.Y > end2d.Y, start2d.Y, end2d.Y))
    
                e.Graphics.DrawLine(linePen, start2d, end2d)
            End Using
        End Sub
    
        Sub New()
            Me.EndPoint = New Point3d(0, 0, 0)
            Me.LineColor = System.Drawing.Color.Black
            Me.StartPoint = New Point3d(0, 0, 0)
        End Sub
    
        Sub New(ByVal point1 As Point3d, ByVal point2 As Point3d)
            Me.EndPoint = point2
            Me.LineColor = System.Drawing.Color.Black
            Me.StartPoint = point1
        End Sub
    
        Sub New(ByVal x1 As Integer, ByVal y1 As Integer, ByVal z1 As Integer, ByVal x2 As Integer, ByVal y2 As Integer, ByVal z2 As Integer)
            Me.EndPoint = New Point3d(x2, y2, z2)
            Me.LineColor = System.Drawing.Color.Black
            Me.StartPoint = New Point3d(x1, y1, z1)
        End Sub
    
    End Class
    However, whenever I try to use the code like this:
    Code:
    Dim line As Line3d = New Line3d(New Point3d(0, 0, 0), New Point3d(150, 150, 0))
    line.LineColor = Color.Red
    
    Me.Controls.Add(line)
    The control is added to the form, but the line does not appear.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  2. #2
    PowerPoster
    Join Date
    Oct 2010
    Posts
    2,141

    Re: Draw 3d object on 2d surface

    Just a guess, but
    Code:
                end2d.X = If(Me.StartPoint.Z = 0, Me.StartPoint.X, Me.StartPoint.X \ Me.StartPoint.Z)
                end2d.Y = If(Me.StartPoint.Z = 0, Me.StartPoint.Y, Me.StartPoint.Y \ Me.StartPoint.Z)
    maybe should be

    Code:
                end2d.X = If(Me.EndPoint .Z = 0, Me.EndPoint.X, Me.EndPoint.X \ Me.EndPoint.Z)
                end2d.Y = If(Me.EndPoint.Z = 0, Me.EndPoint.Y, Me.EndPoint.Y \ Me.EndPoint.Z)

  3. #3
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Draw 3d object on 2d surface

    More than a guess, for me (as I did test).
    In addition to what TnTinMN noted, you also don't size the control anywhere.
    So, to see the red line, I also sized the control by adding to lines to the constructor.
    Code:
      Sub New(ByVal point1 As Point3d, ByVal point2 As Point3d)
        Me.EndPoint = point2
        Me.LineColor = System.Drawing.Color.Black
        Me.StartPoint = point1
        Me.Width = EndPoint.X - StartPoint.X
        Me.Height = EndPoint.Y - StartPoint.Y
      End Sub

  4. #4

    Thread Starter
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,760

    Re: Draw 3d object on 2d surface

    Quote Originally Posted by passel View Post
    More than a guess, for me (as I did test).
    In addition to what TnTinMN noted, you also don't size the control anywhere.
    So, to see the red line, I also sized the control by adding to lines to the constructor.
    I am setting the size in the OnPaint event before I draw the line, it looks like I'm setting the size wrongly though. But what I noticed(after TnTinMN pointed out that I had the wrong names in the end2d) is that whenever I try to run my OnPaint event never gets fired, even if I call line.Refresh(or invalidate).
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  5. #5

    Thread Starter
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,760

    Re: Draw 3d object on 2d surface

    I've updated the class a bit, so here is an update:

    Code:
    Option Strict On
    Option Explicit On
    Public Class Line3d
        Inherits System.Windows.Forms.Control
    
    #Region "Properties"
    
        Private ePoint As Point3d
        Public Property EndPoint As Point3d
            Get
                Return ePoint
            End Get
            Set(ByVal value As Point3d)
                If value.X <> ePoint.X OrElse value.Y <> ePoint.Y OrElse value.Z <> ePoint.Z Then
                    ePoint = value
                    RaiseEvent EndPointChanged(Me, EventArgs.Empty)
                End If
            End Set
        End Property
    
        Private lColor As System.Drawing.Color
        Public Property LineColor As System.Drawing.Color
            Get
                Return lColor
            End Get
            Set(ByVal value As System.Drawing.Color)
                If value <> lColor Then
                    lColor = value
                    RaiseEvent LineColorChanged(Me, EventArgs.Empty)
                End If
            End Set
        End Property
    
        Private sPoint As Point3d
        Public Property StartPoint As Point3d
            Get
                Return sPoint
            End Get
            Set(ByVal value As Point3d)
                If value.X <> sPoint.X OrElse value.Y <> sPoint.Y OrElse value.Z <> sPoint.Z Then
                    sPoint = value
                    RaiseEvent StartPointChanged(Me, EventArgs.Empty)
                End If
            End Set
        End Property
    
        <System.ComponentModel.Browsable(False)> _
        Public Overloads Property Location As System.Drawing.Point
    
        <System.ComponentModel.Browsable(False)> _
        Public Overloads Property Size As System.Drawing.Size
    
    #End Region
    
    #Region "Events"
    
        Public Event EndPointChanged(ByVal sender As Object, ByVal e As EventArgs)
        Public Event LineColorChanged(ByVal sender As Object, ByVal e As EventArgs)
        Public Event StartPointChanged(ByVal sender As Object, ByVal e As EventArgs)
    
        Private Sub Line3d_EndPointChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.EndPointChanged
            Call SetSize()
        End Sub
    
        Private Sub Line3d_StartPointChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.StartPointChanged
            Call SetSize()
        End Sub
    
        Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
            MyBase.OnPaint(e)
    
            Using linePen As System.Drawing.Pen = New System.Drawing.Pen(Me.LineColor, 1)
                Dim start2d, end2d As New System.Drawing.Point
    
                start2d.X = If(Me.StartPoint.Z = 0, Me.StartPoint.X, Me.StartPoint.X \ Me.StartPoint.Z)
                start2d.Y = If(Me.StartPoint.Z = 0, Me.StartPoint.Y, Me.StartPoint.Y \ Me.StartPoint.Z)
                end2d.X = If(Me.EndPoint.Z = 0, Me.EndPoint.X, Me.EndPoint.X \ Me.EndPoint.Z)
                end2d.Y = If(Me.EndPoint.Z = 0, Me.EndPoint.Y, Me.EndPoint.Y \ Me.EndPoint.Z)
    
                e.Graphics.DrawLine(linePen, start2d, end2d)
            End Using
        End Sub
    
    #End Region
    
    #Region "Methods"
    
        Private Sub SetSize()
            Dim start2d, end2d As New System.Drawing.Point
    
            start2d.X = If(Me.StartPoint.Z = 0, Me.StartPoint.X, Me.StartPoint.X \ Me.StartPoint.Z)
            start2d.Y = If(Me.StartPoint.Z = 0, Me.StartPoint.Y, Me.StartPoint.Y \ Me.StartPoint.Z)
            end2d.X = If(Me.StartPoint.Z = 0, Me.StartPoint.X, Me.StartPoint.X \ Me.StartPoint.Z)
            end2d.Y = If(Me.StartPoint.Z = 0, Me.StartPoint.Y, Me.StartPoint.Y \ Me.StartPoint.Z)
    
            Me.Size = New System.Drawing.Size(If(start2d.X > end2d.X, start2d.X - end2d.X, end2d.X - start2d.X), _
                                              If(start2d.Y > end2d.Y, start2d.Y - end2d.Y, end2d.Y - start2d.Y))
        End Sub
    
    #End Region
    
    #Region "New Constructors"
    
        Sub New()
            Me.EndPoint = New Point3d(0, 0, 0)
            Me.LineColor = System.Drawing.Color.Black
            Me.StartPoint = New Point3d(0, 0, 0)
    
            Call SetSize()
        End Sub
    
        Sub New(ByVal point1 As Point3d, ByVal point2 As Point3d)
            Me.EndPoint = point2
            Me.LineColor = System.Drawing.Color.Black
            Me.StartPoint = point1
    
            Call SetSize()
        End Sub
    
        Sub New(ByVal x1 As Integer, ByVal y1 As Integer, ByVal z1 As Integer, ByVal x2 As Integer, ByVal y2 As Integer, ByVal z2 As Integer)
            Me.EndPoint = New Point3d(x2, y2, z2)
            Me.LineColor = System.Drawing.Color.Black
            Me.StartPoint = New Point3d(x1, y1, z1)
    
            Call SetSize()
        End Sub
    
    #End Region
    
    End Class
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  6. #6
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Draw 3d object on 2d surface

    Quote Originally Posted by dday9 View Post
    I am setting the size in the OnPaint event before I draw the line, it looks like I'm setting the size wrongly though. But what I noticed(after TnTinMN pointed out that I had the wrong names in the end2d) is that whenever I try to run my OnPaint event never gets fired, even if I call line.Refresh(or invalidate).
    Yes, the OnPaint event won't fire if the control is 0,0 in size since there is no area to paint.

  7. #7
    Frenzied Member
    Join Date
    Jul 2011
    Location
    UK
    Posts
    1,335

    Re: Draw 3d object on 2d surface

    OK. I don't know what's going on here.

    Setting Me.Size to a new size doesn't work. There are 2 Size properties listed in Intellisense; one for the user control class, and one (I think) for the base control class.

    Using Me.Size = New Size(blah) sets the user control Size, but not the base control Size. It doesn't affect the ClientSize or Bounds properties, they are left with all zero values, so the control never paints.

    Using MyBase.Size = New Size(blah) has the opposite effect, and the user control paints properly.


    Confusingly (to me), using
    Code:
    Me.Width = Math.Abs(start2d.X - end2d.X)
    Me.Height = Math.Abs(start2d.Y - end2d.Y)
    does seem to work, though.

    So why MyBase for Size and Me for Width/Height?

  8. #8

    Thread Starter
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,760

    Re: Draw 3d object on 2d surface

    So why MyBase for Size and Me for Width/Height?
    Ahh... I overloaded Me.Size. This is probably why. I changed the method to change the MyBase.Size rather than Me.Size and it works as expected. Thanks y'all.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  9. #9
    Frenzied Member
    Join Date
    Jul 2011
    Location
    UK
    Posts
    1,335

    Re: Draw 3d object on 2d surface

    Quote Originally Posted by dday9 View Post
    Ahh... I overloaded Me.Size.
    Hmmm. I missed that. Par for the course these days . Must be getting old

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width