Drawing curves using points captured by mouse clicks...

perps

Member
Joined
Sep 8, 2005
Messages
14
Programming Experience
Beginner
Greetings,

Just discovered this forum, appears to be quite the resource indeed!!

I was wondering if anyone could shed some light on a problem i'm having.

I've set up a points array to capture mouse co-ordinates for use in drawing a curve to the canvas. It's working OK, except for the fact that the origin seems to be included in the array somehow, resulting in all the curves being anchored to the top left hand corner of the canvas. The rest of the curve is drawn correctly through the points that have been captured. Does anyone know what might be causing this problem?? Fragments of code attached below...

VB.NET:
	Dim curvePoints(50) As Point
	Dim curveCount As Integer = 0

If (e.Button = MouseButtons.Left) Then
				curvePoints(curveCount).X = e.X
				curvePoints(curveCount).Y = e.Y

				curveCount = curveCount + 1

			ElseIf (e.Button = MouseButtons.Right) Then
			    g.DrawCurve(linePen, curvePoints, tension)
				curveCount = 0
			End If

Thanks in advance,

- perps
 
Hey perps,

I'm not all together up on GDI+ general, however the only thing I can see which might cause your problem is that you have declared 50 element in CurvePoints, your prog will add a new point into however many you click but unless you fill the whole fifty the remaining elements will all be (0,0) thus adding n points at the top left of the form. This might not be right though, not sure how DrawCurve handles the array. Also your curveCount variable will never get past 1 as it's being re-dimmed on every mouseclick event, try making it static, ie

Dim curveCount As Integer = 0
becomes:
static curveCount as Integer

(Ignore this last bit, guessing you actually have them declared as class wide variables, just me being dim)
 
Hey BadgerByte,

Thanks for the reply.

You're right, I do have curveCount declared as a class wide variable.

I also tried using the drawCurve method you posted, unfortunately it yielded the same result.

What you mentioned with regard to all other entries being (0, 0) might be the problem. I've tried setting the array size to be redeclared everytime the user hits the mouse but this still seems to have the curve anchored to the top left hand corner (it also only draws the curve between (0, 0) and the last point clicked for some reason).

What i can't understand is why (0, 0) seems to be the first point in the array, no matter how i declare it or what method i use...??
 
Draw Curves

can i suggest you a different approach how to draw curves on the canvas (actually i made a code to draw onto pictureBox but you can easy adapt it to draw on another control too) ... maybe this is not happiest solution but it works for me.

VB.NET:
[size=2][color=#0000ff]Private[/color][/size][size=2] myPoints [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]New[/color][/size][size=2] ArrayList
[/size][size=2][color=#0000ff]Function[/color][/size][size=2] GetGraphicsObject() [/size][size=2][color=#0000ff]As[/color][/size][size=2] Graphics
 
[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] bmp [/size][size=2][color=#0000ff]As[/color][/size][size=2] Bitmap
 
bmp = [/size][size=2][color=#0000ff]New[/color][/size][size=2] Bitmap(PictureBox1.Width, PictureBox1.Height)
 
PictureBox1.Image = bmp
 
[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] G [/size][size=2][color=#0000ff]As[/color][/size][size=2] Graphics
 
G = Graphics.FromImage(bmp)
 
[/size][size=2][color=#0000ff]Return[/color][/size][size=2] G
 
[/size][size=2][color=#0000ff]End [/color][/size][size=2][color=#0000ff]Function
 
 
 
[/color][/size][size=2]
 
[/size][size=2][color=#0000ff]Private [/color][/size][size=2][color=#0000ff]Sub[/color][/size][size=2] PictureBox1_MouseDown([/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] sender [/size][size=2][color=#0000ff]As [/color][/size][size=2][color=#0000ff]Object[/color][/size][size=2], [/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] e [/size][size=2][color=#0000ff]As[/color][/size][size=2] System.Windows.Forms.MouseEventArgs) [/size][size=2][color=#0000ff]Handles[/color][/size][size=2] PictureBox1.MouseDown
 
[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] myDrawPoints(0) [/size][size=2][color=#0000ff]As[/color][/size][size=2] PointF
 
[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] myPoint [/size][size=2][color=#0000ff]As[/color][/size][size=2] Point
 
[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] i [/size][size=2][color=#0000ff]As[/color][/size][size=2][color=#0000ff]Integer
 
[/color][/size][size=2]
 
[/size][size=2][color=#0000ff]For[/color][/size][size=2] i = 0 [/size][size=2][color=#0000ff]To[/color][/size][size=2] myPoints.Count - 1
 
[/size][size=2][color=#0000ff]If[/color][/size][size=2] i > 0 [/size][size=2][color=#0000ff]Then
 
[/color][/size][size=2][color=#0000ff]ReDim [/color][/size][size=2][color=#0000ff]Preserve[/color][/size][size=2] myDrawPoints(i)
 
[/size][size=2][color=#0000ff]End [/color][/size][size=2][color=#0000ff]If
 
[/color][/size][size=2][color=#008000]'here just take the current point from myPoints arraylist
 
[/color][/size][size=2]myPoint = myPoints.Item(i)
 
 
 
myDrawPoints(i).X = myPoint.X
 
myDrawPoints(i).Y = myPoint.Y
 
[/size][size=2][color=#0000ff]Next
 
[/color][/size][size=2][color=#0000ff]Dim[/color][/size][size=2] G [/size][size=2][color=#0000ff]As[/color][/size][size=2] Graphics
 
G = GetGraphicsObject()
 
G.SmoothingMode = Drawing.Drawing2D.SmoothingMode.AntiAlias
 
[/size][size=2][color=#0000ff]Dim[/color][/size][size=2] pen [/size][size=2][color=#0000ff]As [/color][/size][size=2][color=#0000ff]New[/color][/size][size=2] Pen(Color.Tomato)
 
G.DrawCurve(pen, myDrawPoints)
 
[/size][size=2][color=#0000ff]End [/color][/size][size=2][color=#0000ff]Sub
 
[/color][/size][size=2][color=#0000ff]Private [/color][/size][size=2][color=#0000ff]Sub[/color][/size][size=2] PictureBox1_MouseMove([/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] sender [/size][size=2][color=#0000ff]As [/color][/size][size=2][color=#0000ff]Object[/color][/size][size=2], [/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] e [/size][size=2][color=#0000ff]As[/color][/size][size=2] System.Windows.Forms.MouseEventArgs) [/size][size=2][color=#0000ff]Handles[/color][/size][size=2] PictureBox1.MouseMove
 
myPoints.Add([/size][size=2][color=#0000ff]New[/color][/size][size=2] Point(e.X, e.Y))
 
[/size][size=2][color=#0000ff]End [/color][/size][size=2][color=#0000ff]Sub[/color][/size]

As you can see it is not documented hopefully you will find some sense. Otherwise, just ask for additional help .. would be glad to help you out

Regards ;)
 

Attachments

  • DrawBazier.zip
    22.4 KB · Views: 60
Back
Top