Intersecting lines
Intersecting lines
Intersecting lines
Intersecting lines
Intersecting lines
Intersecting lines Intersecting lines Intersecting lines Intersecting lines Intersecting lines Intersecting lines Intersecting lines Intersecting lines
Intersecting lines Intersecting lines
Intersecting lines
Go Back  Xtreme Visual Basic Talk > > > Intersecting lines


Reply
 
Thread Tools Display Modes
  #1  
Old 01-11-2013, 08:10 PM
CodeCruncher CodeCruncher is offline
Junior Contributor
 
Join Date: Jul 2006
Posts: 355
Default Intersecting lines


Been a while since I have written any code and I am attempting to write an app for general public use, whereby I start with a small drawn circle and keep enlarging it until it hits a boundary line of another irregular shape that surrounds it. The purpose is to determine the closest point of the surrounding shape to the centre of the circle.

So what I need to do is work out is how to tell when the increasing circle touches a line or point of surrounding shape. I can easily tell the distance from that point back to the centre by calculating the radius of the circle just not sure how to determine the contact point. For simplicity purposes if we call the outer shape a rectangle and place the small circle in any spot that is not central.

Appreciate any pointer or code you might be able to provide.
Reply With Quote
  #2  
Old 01-12-2013, 02:10 AM
CodeCruncher CodeCruncher is offline
Junior Contributor
 
Join Date: Jul 2006
Posts: 355
Default

Had a bit of sucess, but the For Loop needs replacing with something dynamic and based off the Rectangle edge.

Code:
Public Class Form1

    'Public Event Paint As PaintEventHandler
    Private pictureBox1 As New PictureBox() With {.Dock = DockStyle.Fill} ', .BackColor = Color.LightBlue}

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Me.Controls.Add(pictureBox1)
        AddHandler pictureBox1.Paint, AddressOf Me.pictureBox1_Paint

    End Sub
Code:
    Private Sub pictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs)

        Dim xLoc As Integer = 120
        Dim yLoc As Integer = 150
        Dim xSize As Integer = 10
        Dim ySize As Integer = 10

        Dim rectangle1 As New Rectangle(xLoc, yLoc, xSize, ySize)
        Dim rectangle2 As New Rectangle(50, 50, 200, 350)

        Dim g As Graphics = e.Graphics
        g.DrawEllipse(Pens.Black, rectangle1)
        g.DrawRectangle(Pens.Red, rectangle2)

        'For syntax not correct...
        'Since the circle is already inside (and touching the rectangle) I cant use "If (rectangle1.IntersectsWith(rectangle2)) Then"
        'I need to stop it when it hits a rectangle Boundary.

        For i = 0 To 13
            xSize += 10
            rectangle1.Width = xSize
            xLoc -= 5
            rectangle1.X = xLoc
            ySize += 10
            rectangle1.Height = ySize
            yLoc -= 5
            rectangle1.Y = yLoc
            g.DrawEllipse(Pens.Black, rectangle1)
        Next
Code:
        Label1.Text = rectangle1.ToString()
        Label2.Text = rectangle2.ToString()

        rectangle1.Intersect(rectangle2)

        rectangle1.X = xLoc
        rectangle1.Y = yLoc

        Label3.Text = rectangle1.Width
        Label4.Text = rectangle1.Height

        If rectangle1.Height < rectangle1.Width Then rectangle1.Width = rectangle1.Height
        If rectangle1.Width < rectangle1.Height Then rectangle1.Height = rectangle1.Width
        e.Graphics.FillEllipse(Brushes.Green, rectangle1)

    End Sub

End Class
Attached Images
File Type: jpg Intersect.jpg (22.0 KB, 19 views)
Reply With Quote
  #3  
Old 01-12-2013, 10:52 AM
Roger-U Roger-U is offline
Centurion
 
Join Date: Apr 2008
Location: Surrey, UK
Posts: 111
Default

Why not just calculate the distance of each bounding line from the centre point - and the shortest one will be the radius of the circle.

The calculation is pretty easy.. http://en.wikipedia.org/wiki/Distanc...oint_to_a_line
Reply With Quote
  #4  
Old 01-12-2013, 08:47 PM
CodeCruncher CodeCruncher is offline
Junior Contributor
 
Join Date: Jul 2006
Posts: 355
Default

Roger-U I think your definition of pretty easy and mine differs hugely

My math skills are extremely (and I do mean extremely) basic, so what is written on that page might as well be written in Greek to me. But I do think it might be my only option to do this properly.

As I say I don't understand the math, or what they are trying to achieve (beyond finding the closest point on the line to the starting point (0,0).

Does that formula find a point mid way along the line should the starting point be above the line bottom point (so the end point is further away)?, or will it only find the closest end of the line?

Imagine for a moment a pentagon (easy irregular shape) with the start point not in the centre, how would this formula check all of the sides to make sure it had found the closest?

Would you need to slowly rotate the formula somehow so it checked different points 360 degrees around?

Sorry if my questions are dumb but without seeing how that formula work in action it is hard to grasp what it is doing.
Reply With Quote
  #5  
Old 01-12-2013, 09:03 PM
CodeCruncher CodeCruncher is offline
Junior Contributor
 
Join Date: Jul 2006
Posts: 355
Default

A picture is worth a thousand words...

I have drawn an orange X on the image to show a different start point.
Based on where I have put the X the line will be closer than either end.

Is the formula capable of calculating this point?
What happens if there are multiple line?
what happens if the line was curved like a circle?
Attached Images
File Type: jpg New Location.jpg (16.0 KB, 23 views)
Reply With Quote
  #6  
Old 01-12-2013, 09:49 PM
CodeCruncher CodeCruncher is offline
Junior Contributor
 
Join Date: Jul 2006
Posts: 355
Default

In for a penny in for a pound...

While trying to get my head around the formula, I think perhaps I should try reach my end goal now which is to measure these points in 3D. As understanding it in 3D shouldn't be much harder than understanding it in 2D. Once I have the principle down pat it shouldn't be too hard to work out 3D.

I do understand how the Cartesian coordinates work in three dimensions, so that a least gives me a toe in the water. Never having learned it at school, I picked it up when I started coding.

The bit that has always confused me is where the a, b & c come from. I suspect these are some arbitrary numbers assigned to the X, Y and Z axis.
a on the X axis, b on the Y axis and c on the Z axis?

Edit:
Found this on spherical coordinates http://en.wikipedia.org/wiki/Spherical_coordinates which might be the way to go.

Last edited by CodeCruncher; 01-12-2013 at 10:07 PM.
Reply With Quote
  #7  
Old 01-13-2013, 04:40 AM
Roger-U Roger-U is offline
Centurion
 
Join Date: Apr 2008
Location: Surrey, UK
Posts: 111
Default

I think maybe you need to understand the maths first.

1. Your shape is defined by line segments i think (i.e. all straight edges, no curves?)

2. if you know the coordinates of the 'corners' then each adjacent pair of points (the corners) defines a straight line

3. below is a VB version of the Java program in the wikipedia link above. Call it for each line segment and record the smallest return value

BUT! Are all your polygons concave? - google concave polygon if you don't know what that means.

Assuming they are, then will your circle centre point always be inside the polygon?

if either of those is not true then you will also need to check if the returned value is off the end of the actual line segment, as the calculation assumes the line in infinitely long.


Code:
  Public Function pointToLineDistance(ByVal A As PointF, ByVal B As PointF, ByVal P As PointF) As Double

    Dim h As Double
    h = ((A.X - B.X) ^ 2) + ((A.Y - B.Y) ^ 2)
    h = Math.Sqrt(h)

    Return Math.Abs(((P.X - A.X) * (B.Y - A.Y) - (P.Y - A.Y) * (B.X - A.X)) / h)


  End Function

I think going to 3D will not be as simple as you imagine!! Will the sphere touch the edges or the surfaces... how will you decide which points define the (flat) surface? If it is a cube then each surface will have 4 corners, but any 3 will define that face... good luck
Reply With Quote
  #8  
Old 01-15-2013, 10:54 PM
CodeCruncher CodeCruncher is offline
Junior Contributor
 
Join Date: Jul 2006
Posts: 355
Default

Thanks Roger-U (keep having a chuckle to myself every time I type that, as most people on a US forum probably won’t get it)…
I did a search for some 3d code image examples and found one basic enough that I might be able to step through the code and learn how it works.
I went through and changed a lot of the code to simplify it, but I think I still might need to do some GUI enhancements to tell it which lines to evaluate.
Walls will be more or less straight, even if it is a circle i.e. will make a round tube, not curved sides like a wine barrel.
If I can define the lines I want to measure i.e. possibly make them yellow (like the edited picture) perhaps I use the colour part of the control to calculate the lengths.
Attached Images
File Type: jpg 3D Graphics.jpg (56.3 KB, 22 views)
Reply With Quote
  #9  
Old 01-16-2013, 12:55 PM
Gruff's Avatar
GruffIntersecting lines Gruff is offline
Bald Mountain Survivor

Retired Moderator
* Expert *
 
Join Date: Aug 2003
Location: Oregon, USA - deceased
Posts: 6,440
Default

As Roger stated locating 3D interference is not a trivial task. I would not to want to have to write one myself.

Regarding 2D: Here is some code I wrote some time ago that will locate the closest point in a list of lines to a given point. Note we define a new class of Line2D to do the job.

Code:
Public Class Line2D Public Property sPt As PointF Public Property ePt As PointF Public Sub New(StartPt As PointF, EndPt As PointF) sPt = StartPt ePt = EndPt End Sub End Class Public Class Form1 Dim myLines as New List(of Line2D) ' Fill Line list with lines using the new method here ' Example: myLines.Add(New Line2D(New PointF(x1,y1), NewPointF(X2,Y2)) ' Get circle center point (CenPt) here Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click ' Get largest circle radius endpoint. ' If what you really want is the largest circle radius then that can be ' returned instead Dim ClosestPoint as PointF = ClosestPointOfAllEdges(CenPt, myLines) messagebox.show(ClosestPoint.X & ", " & ClosestPoint.Y) End Sub ' --- Supporting Routines --- Public Function ClosestPointOfAllEdges(ByVal Pt as PointF, ByRef as List(of Line2Ds)) as PointF ' Task: Loop through all line objects locate the closest point on the line to the given point Dim xPt as PointF = Nothing ' Get first distance. Dim ClosestDist as single = Closet_Dist(Pt, Line2Ds(0).sPt, Lines2Ds(0).ePt, xPt) Dim ClosestPt as PointF = xPt ' Loop through all lines For each oLine as Line2D in Lines2Ds Dim CurrDist as single = Closet_Dist(Pt, oLine.sPt, oLine.ePt, xPt) is CurrDist < ClosestDist then ClosestDist = CurrDist ClosestPt = xPt End if Next Return ClosestPt End Function Public Function Closest_Dist(ByVal Pt As PointF, ByVal sPt As PointF, ByVal ePt As PointF, ByRef OnPt As PointF) As Single 'Task: Given a line(xa,ya)(xb,yb) and a point(x,y) ' Determine the closest distance between the line and the point. ' Although the line is defined by a pair of x,y points, this function makes no ' such distinction. The line is assumed infinite in length. 'Step 1 Check if the line is vertical or horizontal. Solve if true. 'Step 2 Determine a line perpendicular to the existing line and through the point. 'Step 3 Determine the intersection point of both lines. Dim Sab, Scd, Iab, Icd, XDist, YDist, Dist As Single If IsEqual(sPt.X, ePt.X) Then Dist = Math.Abs(sPt.X - Pt.X) 'Line is vertical OnPt.X = sPt.X 'Line is vertical OnPt.Y = Pt.Y ElseIf IsEqual(sPt.Y, ePt.Y) Then Dist = Math.Abs(sPt.Y - Pt.Y) 'Line is horizontal OnPt.X = Pt.X 'Line is horizontal OnPt.Y = sPt.Y Else Sab = (sPt.Y - ePt.Y) / (sPt.X - ePt.X) 'Slope of the existing line Iab = sPt.Y - (Sab * sPt.X) 'Y intercept of the existing line Scd = -(1 / Sab) 'Slope of the calculated the line (Inverse of existing.) Icd = Pt.Y - (Scd * Pt.X) 'Y intercept of calculated line OnPt.X = CSng((Icd - Iab) / (Sab - Scd)) 'Calculate Intersection point of both lines. OnPt.Y = CSng((Sab * OnPt.X) + Iab) XDist = Math.Abs(OnPt.X - Pt.X) 'Calculate X and Y Spacing YDist = Math.Abs(OnPt.Y - Pt.Y) 'Find the true distance (Hypotnuse of Xdist and Ydist). Dist = CSng(Hypotnuse(XDist, YDist)) 'Hypotnuse of Xdist and Ydist. End If Return Dist End Function Public Function IsEqual(ByVal Float1 As Single, ByVal Float2 As Single) As Boolean 'As floating point math answers do not always resovle to exactly zero 'we have to have a "close enough" approximation Const Tolerance As Single = 0.000000001 Return Math.Abs(Float1 - Float2) < Tolerance End Function Public Function Hypotnuse(ByVal x As Single, ByVal y As Single) As Single Return CSng(Math.Sqrt((x ^ 2) + (y ^ 2))) End Function End Class
__________________
Burn the land and boil the sea
You can't take the sky from me


~T

Last edited by Gruff; 01-16-2013 at 01:05 PM.
Reply With Quote
  #10  
Old 01-16-2013, 06:08 PM
CodeCruncher CodeCruncher is offline
Junior Contributor
 
Join Date: Jul 2006
Posts: 355
Default

Thank you both for your input. To simplify matters I have gone away from my initial idea of creating a circle that expands from the point of reference until it hits a boundary line. I was primarily thinking along the lines of doing this as a means of easily working out where it touches the line.
But as I quickly discovered while it is graphically not that hard to do in 2D, it was not easy to return a value of that point.

Instead I have decided to ditch the idea of the expanding circles as there is no real benefit beyond a visual one. Instead I have decided to simplify my approach. But before I explain the new direction I really should explain the purpose of the application to help guide advice.
The whole reason I need to be able to measure from a fixed point to anywhere on a line is because I am writing a free to public app for working out speaker cabinet shapes, and the effect it will have on baffle step (the point where bass volume level starts to drop off because of the shape of the box)

Most speakers are based on a flatish front panel (2D baffle) and the shortest edge of the front panel determines that bass roll off point. However when working with open baffle speakers (a non-enclosed speaker) often the shape can be more complex and 3 dimensional.
Basically a U frame is a 3D cube with one side missing / empty. The speaker driver sits in the side opposite the opening, so the length that needs to be worked out is from the centre of the speaker driver to the open edge (driver may or may not be central).

I have also been thinking rather than trying to find the shortest point, I really should expand my original idea to model the whole shape of the baffle or opening to provide a more complete picture of the bass roll off, if I was to put the speaker driver in one corner while I may have one length that is short, there may be many others that are not, and to model it properly I need to take all points into consideration, so what I feel I need to do is start from 12 o’ clock and work around the whole surface / opening and capture the distances for multiple points, whether that be every degree, or every couple of degrees is yet to be determined.

I am sure some if not all of the code already provided can get me close to the new direction, so thank you for your contributions.
Reply With Quote
  #11  
Old 01-17-2013, 10:09 AM
Gruff's Avatar
GruffIntersecting lines Gruff is offline
Bald Mountain Survivor

Retired Moderator
* Expert *
 
Join Date: Aug 2003
Location: Oregon, USA - deceased
Posts: 6,440
Default

Your idea of using an expanding circle was flawed from the start.
I could go into it with pages of why it won't work, but essentially you are working with
floating point math. which as it turns out is not exact plus the fact you would have to check every possible point around the circle that a hit might take place which would be infinite. This is even more an issue with a sphere and a bounding 3D shape.

For 2D as Roger pointed out it is much easier to loop through your line segments and calculate the closest point and distance from the line to your circle center point.
There is only one solution for each line. The code I posted does exactly that.

See attached Picture
Attached Images
File Type: png Closest Point Solution.png (167.6 KB, 23 views)
__________________
Burn the land and boil the sea
You can't take the sky from me


~T
Reply With Quote
  #12  
Old 01-17-2013, 10:28 AM
Gruff's Avatar
GruffIntersecting lines Gruff is offline
Bald Mountain Survivor

Retired Moderator
* Expert *
 
Join Date: Aug 2003
Location: Oregon, USA - deceased
Posts: 6,440
Default

Also as Roger pointed out If your bounding shape is not convex then you need to check the vertex points as well and see if your closest point is at a vertex.

(See Picture)
Attached Images
File Type: png Closest Vertex.png (161.7 KB, 18 views)
__________________
Burn the land and boil the sea
You can't take the sky from me


~T
Reply With Quote
  #13  
Old 01-17-2013, 05:04 PM
CodeCruncher CodeCruncher is offline
Junior Contributor
 
Join Date: Jul 2006
Posts: 355
Default

I am still formulating the idea of what I want to measure… when I first started the idea was based around the closest point, because that is the point at which the sound level starts to roll off. But upon further consideration an average of points is going to be a much more revealing piece of information that governs the roll off pattern.

For example if we have a closest point of 10, and we have numerous other points of say 70,80,90 & 50 we have an average of 60, which will produce a more accurate average point of roll off. A speaker driver radiates sound in a 360 degree pattern and it will hit numerous baffle edges in its journey away from the driver, that in turn means it will roll off at different frequencies for every different length.

The roll off is not sudden like a light switch, it is a gradual process based on the various baffle length edges. Which makes me think I need to be measuring more like a pin wheel, with pre-defined steps around the full 360 degrees. If I was to measure every degree I could sum and average 360 points, if I was to measure every 6 degrees I could sum and average 60 points and so on depending on how accurate I need it to be.

Having a fixed amount of measurement points at fixed angles should also make for a more simplified collection of information (by knowing the actual size of the array, and the direction to be measured). If I was to store each of the length values in an array I could also create a separate chart to plot the roll off rate.

For example if I had the start point in the centre of a circle, every measurement would be identical, and the roll off point length would be equal to the radius. The sound roll off would be sharp because all lengths are equal therefore the they would roll at the same frequency creating a sudden drop in volume.
Whereas if I had a square of the same size as the previous circle example it would have a more gradual roll off of volume as the corners are a greater in length than the closest side. Both shapes would produce a different graph of roll off slope.

I haven’t evaluated the provided code yet because I am still trying to come to grips with the code example I found on the web. Once I understand how the 3D object is drawn it will help me understand the code you have both kindly provided. Attached is a copy of the sample code with a few changes to the variable names and small code tidy up to help me identify what type of variable I am playing with (helps when trying to understand a new concept). I also mapped the coordinates in the sample file last night to see what is being drawn to screen. This also helps understand the logic of how it is being drawn to screen.
Attached Images
File Type: jpg Points.jpg (90.8 KB, 18 views)
Attached Files
File Type: zip GraphicsDisplay.zip (108.2 KB, 34 views)
Reply With Quote
  #14  
Old 01-17-2013, 05:32 PM
CodeCruncher CodeCruncher is offline
Junior Contributor
 
Join Date: Jul 2006
Posts: 355
Default

Just took a first look at the drawing I posted earlier (finished plotting and went to bed).

It would appear that the provided data file is typed in back to front. I think the coordinates are typed in Z, Y, X.

That should be easy enough to flip around in the code to put them in normal order (will need to flip the data file as well).
Attached Images
File Type: jpg Points2.jpg (91.2 KB, 18 views)
Reply With Quote
  #15  
Old 01-17-2013, 08:57 PM
CodeCruncher CodeCruncher is offline
Junior Contributor
 
Join Date: Jul 2006
Posts: 355
Default

Could someone please run the app I posted and confirm the perspectives for me. Been a very long time since I did drawing at school, but I think who ever coded this thing has Z where it should be Y and Y doesn't look right to me at all. Thanks...

Edit:
Based on certain words in the code, this code would seem to be based on linear algebra, which I don't know the first thing about, but i did find this ebook online and was wondering if it might be worth while buying?

http://www.dummies.com/store/product...470538163.html

Last edited by CodeCruncher; 01-17-2013 at 09:18 PM.
Reply With Quote
  #16  
Old 01-18-2013, 04:15 AM
surfR2911 surfR2911 is offline
Contributor
 
Join Date: Oct 2009
Posts: 719
Default The first VS.Net 2012 Desktop 3D graphics code attachment I've seen..

Quote:
Originally Posted by CodeCruncher
Could someone please run the app I posted..
Congradulations CodeCruncher, it looks like you've posted the first VS.Net 2012 Desktop code attachment,
(at least that's 3D graphics related, which is the kind of stuff I'm interested in).

Unfortunately I tried to open the sln file using VS.Net 2010 Express and was unable to do so (see screenshot attachment).

So for those who find this thread and are looking for 3D drawing projects they can load ing VB.Net 2010
I zipped up the three such projects from my Viewer thread and attached them.
They demonstrate:
1.) Drawing 3D wireframe sphere
2.) Drawing a 3D cube with greyscale faces and controlled rotation in three dimensions at runtime
3.) Drawing a 3D cube with colored faces and realtime animated rotation

However, I figure that where's there is one VB.Net 2012 Desktop code attachment, there is bound to more in the future,
so to be able to help people out like CodeCurncher I installed the VS.Net 2012 Express Desktop version (45 mintues, 2 reboots)
and was finally able to open/view his solution/project.

I have no idea if you are doing the translation correctly but I tweaked things a little
to get a slightly nice looking drawing (see other attached screenshot) than the screenshot in post#8 using this code:
Code:
Private Sub PanellDraw(ByVal sender As System.Object, _
                       ByVal e As System.Windows.Forms.PaintEventArgs) Handles PanelDraw.Paint
    LowerUpperLimit()
    Dim sngSF As Single, sngX1 As Single = p3dLow.dblX * 1.5
    Dim sngY1 As Single = p3dLow.dblY * 1.5, sngX2 As Single = p3dUp.dblX * 1.5
    Dim sngY2 As Single = p3dUp.dblY * 1.5
    Dim CX As Single = sngX1 + (sngX2 - sngX1) / 2, CY As Single = sngY1 + (sngY2 - sngY1) / 2
    If PanelDraw.Width / (sngX2 - sngX1) < PanelDraw.Height / (sngY2 - sngY1) Then
        ' calculate scale factor of drawing to be displayed based on the bounding box
          sngSF = Fix((PanelDraw.Width / (sngX2 - sngX1)))
          If sngX1 = 0.0 And sngX2 = 0.0 Then sngSF = 1.0
    Else
         sngSF = Fix((PanelDraw.Height / (sngY2 - sngY1)))
        If sngY1 = 0.0 And sngY2 = 0.0 Then sngSF = 1.0
     End If
     If sngSF = 0.0 Then sngSF = 1.0
     Dim sTransform As New Matrix
     e.Graphics.TranslateTransform((PanelDraw.Width / 2 + CX) - 100, _
                                   (PanelDraw.Height / 2 + CY) + 50)      ' center the drawing
     e.Graphics.RotateTransform(180) ' Y upwards. by default Y points down from top left
     e.Graphics.ScaleTransform(sngSF, sngSF) ' transform to scale
     Dim p3dA As New Point3D                 ' draw the required lines
     Dim p3dB As New Point3D
     Dim int As Integer = 1
     ' Create a new Pen object. 
     Dim whitePen As New Pen(Color.White)
     ' Set the width
     whitePen.Width = 0.4F
     ' Set the DashCap to round.
     whitePen.DashCap = Drawing2D.DashCap.Round
     For int = 0 To arlPts.Count - 2
       p3dA = arlPts(int)
       p3dB = arlPts(int + 1)
       ' Set the SmoothingMode property to smooth the line.
       e.Graphics.SmoothingMode = Drawing2D.SmoothingMode.HighQuality
       e.Graphics.DrawLine(whitePen, CSng(p3dA.dblX), CSng(p3dA.dblY), _
                           CSng(p3dB.dblX), CSng(p3dB.dblY))
     Next
End Sub
Note1: Pen.Wdith can be se down to 0.1F for even thinner "ghostly" lines
Note2: The other smoothing option commonly found is Drawing2D.SmoothingMode.AntiAlias
Attached Images
File Type: jpg screenshot_solution_file_newer_version.JPG (14.9 KB, 14 views)
File Type: jpg screenshot_with_thinline_pen-n-smoothmode-highquality.JPG (27.7 KB, 16 views)
Attached Files
File Type: zip VSdotNet_2010_Express_3D_drawing_projects.zip (268.5 KB, 11 views)

Last edited by surfR2911; 01-18-2013 at 04:27 AM.
Reply With Quote
  #17  
Old 01-18-2013, 07:47 PM
CodeCruncher CodeCruncher is offline
Junior Contributor
 
Join Date: Jul 2006
Posts: 355
Default

Wow surfR2911 you work with graphics is quite impressive! I have bookmarked the link to your thread for when I plan to learn advanced graphics.

That’s not my code, I attached a sample I found on the web somewhere (and promptly lost where I found it… ) I’m at the kiddy end of the pool trying to learn how to tread water, not swimming in the Olympics like you .

All I did to that code was change variable names like “matrix” to “arrMtx” so I could aid my learning by knowing I am dealing with an Array. I changed a couple 3 line If statements to run on a single line etc. for easier viewing, but the code is mostly as the original author wrote it.

It seems like a fairly decent basic bit of code to try lean concepts without addition non drawing related code, but I am still in the stages of trying to get a basic understanding of it at the moment, as I know none of the math terms contained in it. I suspect it is some kind of linear algebra based on some of the words I Googled, but I can’t even be sure of that as I never learned algebra so I wouldn’t know it if it hit me.

I suspect from my understanding of the non-related math code that it isn’t a super-efficient in terms of coding, as it takes you through the draw process at start up before even loading a file with graphic coordinates.

I appreciate any assistance you can provide in making the drawing code even easier to understand. Since you have seen the code there are a couple of things you might be able to help me with to understand…

There seems to be a few places where the author has declared things in a rather strange way, for example, every time a new 3D Point is created it runs through this new loop, which to my mind makes no sense at all.

By default a double is going to have a value of 0.0 and they seem to be resetting it to 0.0 which makes no sense. I have commented out the code in New and it works exactly the same, so I see no point to it being there.

Code:
        Public dblX As Double
        Public dblY As Double
        Public dblZ As Double

        Public Sub New()
            dblX = 0.0
            dblY = 0.0
            dblZ = 0.0
        End Sub
Also the “Dim sTransform As New Matrix” Doesn’t seem to be used at all.

But the one that does my head in is this one. int is declared with a value of 1 and then immediately set to 0 in the For loop.

Code:
        Dim int As Integer = 1
        For int = 0 To arlPts.Count - 2
            p3dA = arlPts(int)
            p3dB = arlPts(int + 1)
            e.Graphics.DrawLine(Pens.AliceBlue, CSng(p3dA.dblX), CSng(p3dA.dblY), CSng(p3dB.dblX), CSng(p3dB.dblY))
        Next
Because I am not up to the point where I can write my own version of this code, I start to question my own logic on things which seem completely wrong to me.
If I knew how all the math worked and knew what it was doing, I would feel more confident in saying that code like those above are wrong.

I am not looking to poke holes in this persons code, but when I can’t make sense of the simple logic, it is distracting me from learning the harder stuff.

Thanks for the code edition that is a much nicer displayed image. Did the 2D Y and Z seem to be the right picture for the angle to you? They don’t seem right to me.

Edit:
Yes have used e.Graphics.SmoothingMode = SmoothingMode.AntiAlias in other projects along with SetStyle(ControlStyles.OptimizedDoubleBuffer, True) and SetStyle(ControlStyles.AllPaintingInWmPaint, True)

Not sure what they all do but I had always planned to look the up.

Last edited by CodeCruncher; 01-18-2013 at 09:12 PM.
Reply With Quote
  #18  
Old 01-18-2013, 10:09 PM
CodeCruncher CodeCruncher is offline
Junior Contributor
 
Join Date: Jul 2006
Posts: 355
Default

Running through the various shapes this doesn’t see to alter either, is the For Next loop redundant code?
A new Array arrM3D is created with a size of 4,4 with a type of double, so by default arrM3D will have all available spots auto populated with 0.0
It loops through all 16 spots setting the value to 0 before setting 4 real values, struggling to see the point of the For Next loop if it has one.

Code:
        Public arrM3D As Array

        Public Sub New()

            arrM3D = Array.CreateInstance(GetType(Double), 4, 4)
            For intA As Integer = arrM3D.GetLowerBound(0) To arrM3D.GetUpperBound(0)
                For intB As Integer = arrM3D.GetLowerBound(1) To arrM3D.GetUpperBound(1)
                    arrM3D.SetValue(0, intA, intB)
                Next intB
            Next intA
            arrM3D.SetValue(1, 0, 0)
            arrM3D.SetValue(1, 1, 1)
            arrM3D.SetValue(1, 2, 2)
            arrM3D.SetValue(1, 3, 3)

        End Sub
Reply With Quote
  #19  
Old 01-19-2013, 04:40 AM
passel's Avatar
passelIntersecting lines passel is offline
Sinecure Expert

Super Moderator
* Guru *
 
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 8,024
Default

There are programming languages, such as C, that do not initialize created variables to an initial value, so the initial value is whatever happened to be in the memory allocated.
It is requirement for the programmer to initialize variables to a known state before use.
In VB, since numeric variable values are initialized to 0, if 0 is the initial value you want, then you don't have to explicitly initialize the value.
But, I assume, there are generally three main reasons that you see code that does initial a newly created numeric variable to 0 in VB.
1. It is ported code from a language that did require it, and the person porting did not want to modify the logic in any way.
2. The person feels it is more consistent, clear, or portable to initialize all values regardless of the programming language used.
3. The person is unaware that uninitialized newly created numeric variable values (arrays included) in VB are implicitly set to 0 by the compiler.

As for initializing a value to 1, and then immediately using it as a for loop index, which will reassign it anyway, you're right that it is pointless. But, you'll probably come across a lot of pointless code over a lifetime of programming.
Perhaps, the loop was not originally a For loop, so the index value had to be initialized, but later was changed to a For loop, and the initialization at declaration is now pointless.
And, perhaps the code was derived from another language in which the lower bound of the array was 1, not 0, as a number of languages (i.e. VB6, FORTRAN, Ada, etc) allowed the lower bound of an array to be non-zero.
__________________
There Is An Island Of Opportunity In The Middle of Every Difficulty.
Miss That, Though, And You're Pretty Much Doomed.
Reply With Quote
  #20  
Old 01-19-2013, 06:03 AM
CodeCruncher CodeCruncher is offline
Junior Contributor
 
Join Date: Jul 2006
Posts: 355
Default

Thanks passel. I all too often just assume that it is me that is wrong and because they are writing code I can't write, it must be me that is mistaken, but I guess even smart people have off days...

I have gone through the code again trying to refine some of the loops to make it easier to read, when I noticed a large chunk of the code isn't actually used, so I put a break point at every event, and if it doesn't get used, I am commenting it out.

That way what I need to learn for now to just draw the 2D and 3D shapes to screen will be greatly reduced.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Intersecting lines
Intersecting lines
Intersecting lines Intersecting lines
Intersecting lines
Intersecting lines
Intersecting lines Intersecting lines Intersecting lines Intersecting lines Intersecting lines Intersecting lines Intersecting lines
Intersecting lines
Intersecting lines
 
Intersecting lines
Intersecting lines
 
-->