DrawPolygon - unexpected behaviour

There must be something I’ve done wrong (or simply forgotten). I’ve used DrawPolygon before to draw some simple shapes into the paint event of a canvas and it worked as expected.

It seems as though the xy point of origin is stuck on the top left of the canvas, which is ok and I understand that it is always there, but the line should not draw from there if you change it in code? I’ve mucked around with this for the last 1/2 hour or so - hopefully it’s one of those simple stupid things that we all fall victim to on occasion.

Here’s the code (in the canvas paint event):

[code] // draw ADC symbol ----------------
Dim ADCsymbol (4) As Integer
Dim sx, sy As Integer //set xy origin
sx = 50
sy = 50

g.ForeColor = kBlueMid

ADCsymbol(1) = sx 'x
ADCsymbol(2) = sy 'y
'ADCsymbol(3)=sx+12 'x
'ADCsymbol(4)=sy+12 'y
'ADCsymbol(5)=sx 'x
'ADCsymbol(6)=sy+24 'y


g.DrawPolygon ADCsymbol[/code]

And here’s the result:

Some of the code has been remarked out for testing purposes, but I was expecting to just get a point/ dot at 50,50 yet the line draws from 0,0.

You use 2 co-ordinates but pass an array with 4 values (it appears index 0 is ignored)

The last 2 co-ordinates in your array are both 0
So you are asking for a polygon from 50,50 to 0,0

Use them all and you get want you wanted… this code draws a triangle:

[code] Dim ADCsymbol (6) As Integer
Dim sx, sy As Integer
sx = 50
sy = 50
ADCsymbol(1) = sx
ADCsymbol(3)=sx + 50
ADCsymbol(5)=sx + 50
ADCsymbol(6)=sy + 50

g.DrawPolygon ADCsymbol[/code]

Thanks Jeff,

That triangle code (or similar) is what I’ve used in the past - using just a basic triangle (filled):

[code]Dim p (6) as Integer

g.ForeColor = kBlueMid

p(1)=0 'x
p(2)=0 'y
p(3)=12 'x
p(4)=12 'y
p(5)=0 'x
p(6)=24 'y


Just to change the point of origin seems like a lot of unnecessary covfefe.

I’ll check it out tomorrow.

What Jeff is saying is that your line is being drawn from 50,50 to 0,0, not the other way around. By initializing the array with 4 integer elements, they all start with a value of 0. Then in your code, you’re only changing the first two.

Ok thanks guys - as suggested the issue was with the initializing of the array.

Seems that if too many elements (xy pairs) are defined but not used, the origin and therefore the ‘closed point’ defaults to 0,0.

eg. Draws a Square:

[code] // draw ADC symbol ----------------
Dim ADCsymbol (8) As Integer
Dim sx, sy As Integer //set xy origin
sx = 19
sy = 28

g.ForeColor = kBlueMid

ADCsymbol(1)= sx 'x
ADCsymbol(2)= sy 'y
ADCsymbol(3)= sx+20 'x
ADCsymbol(4)= sy 'y
ADCsymbol(5)= sx+20 'x
ADCsymbol(6)= sy+20 'y
ADCsymbol(7)= sx 'x
ADCsymbol(8)= sy+20 'y


g.DrawPolygon ADCsymbol[/code]

The image to the left is correct but if the code is: Dim ADCsymbol (10) As Integer,
Then we end up with the image on the right.

When I first started testing this, I defined more elements than needed simply because I wasn’t sure exactly how many points I would need - erroneously thinking that it wouldn’t matter.

All sorted now - cheers.