Not getting straight line with simple algorithm

Dunno whats up but

  1. create a new desktop project
  2. put a canvas on the default window and make its width = height (keeps things simple)
  3. put a text field on the window
  4. in the text fields textchange event put “canvas1.invalidate”
  5. in the paint event of the canvas put

[code]
dim pi as double = 3.14159265358979323846264338327950
dim r, x, y, hw, hh, a As double

hw = me.Width * 0.5
hh = me.Height * 0.5

r = hw

a = val(textfield1.text) * pi / 180.0
x = r * Cos(a)
y = r * Sin(a)

g.DrawLine(hw, hh, hw + x, hh + y)[/code]

No special cases for 0, 90, 180, 270 etc
Run

This gives me nice straight lines vertically & horizontally at 0, 90, 180, and 270

At such a size, jaggies take the best of any algorithm by the quantum effects they produce. Especially close to vertical or horizontal. Have you tried drawing on a much larger picture (some like 8 times large) and scaling it ?

I hac[quote=139911:@Norman Palardy]Dunno whats up but

  1. create a new desktop project
  2. put a canvas on the default window and make its width = height (keeps things simple)
  3. put a text field on the window
  4. in the text fields textchange event put “canvas1.invalidate”
  5. in the paint event of the canvas put

[code]
dim pi as double = 3.14159265358979323846264338327950
dim r, x, y, hw, hh, a As double

hw = me.Width * 0.5
hh = me.Height * 0.5

r = hw

a = val(textfield1.text) * pi / 180.0
x = r * Cos(a)
y = r * Sin(a)

g.DrawLine(hw, hh, hw + x, hh + y)[/code]

No special cases for 0, 90, 180, 270 etc
Run

This gives me nice straight lines vertically & horizontally at 0, 90, 180, and 270[/quote]
I did this. Experimented with different sizes. On Windows 270 degrees does not give me a straight line. thanks

[quote=139911:@Norman Palardy]Dunno whats up but

  1. create a new desktop project
  2. put a canvas on the default window and make its width = height (keeps things simple)
  3. put a text field on the window
  4. in the text fields textchange event put “canvas1.invalidate”
  5. in the paint event of the canvas put

[code]
dim pi as double = 3.14159265358979323846264338327950
dim r, x, y, hw, hh, a As double

hw = me.Width * 0.5
hh = me.Height * 0.5

r = hw

a = val(textfield1.text) * pi / 180.0
x = r * Cos(a)
y = r * Sin(a)

g.DrawLine(hw, hh, hw + x, hh + y)[/code]

No special cases for 0, 90, 180, 270 etc
Run

This gives me nice straight lines vertically & horizontally at 0, 90, 180, and 270[/quote]
Ah, certain sizes seem to be perfectly straight and not particularly with an even sized canvas.

Yes that the nature of an algorithm the describes something ideal that then has to be mapped into a display that has discrete pixels.
You get jaggies.

In an ideal mathematical world a line basically has no width. Its described by two points (in 2 or 3 dimensions).
But on screen it has some width.
And each pixel position is (or ideally) should be a floating point value like x=1.234567 y=1.8765
But when you draw that pixels are at positions (1,1), (1,2), (2,1) etc
And there has to be a mapping between those floating point values & actual pixels
So you get a point (1.234567, 1.8765) mapped onto the pixel at (1,2)
Anti aliasing is a technique to make these jaggies less pronounced

That why the lines are not perfectly straight at certain angles

I just tried it there and it looks like its anti aliasing that’s lacking

I don’t know if you can fix it in Xojo, but its normal behaviour creating out of the box lines, this is from a abandoned project to draw hotspots for a game engine and get the coordinates, the lines are jagged but it didn’t really matter in this case as they were guides more than anything

Have you tried turning on GDI+ for the Windows build?

That seems to improve things :slight_smile:

270 isn’t straight up because of floating point inaccuracy.

Mathematically Cos(270*pi/180) = 0 but in the computer pi and Cos aren’t exact and you get…

Cos(270*pi/180) = -0.00000000000000018370

Combine that slight negative with hw and it can push the pixel coordinate 1 less than what hw is, depending. If the Canvas is even width, like 300, then hw is cleanly 150. Adding that to x will plot at 149. But an odd width Canvas, 301, gives hw of 150.5 and then when added to x both will round to 150 yielding a pixel perfect vertical line.

Actually, what’s happening is that double->integer conversion doesn’t Round, it Floors. So a hw of 150.0 when added to an x of -0.0000000001 gives 149.999999999 which floors to 149 and causes the vertical tilt.

Anyways, here’s some ideas to fix it…

  1. Round x and y or Round after adding to hw, hh.

  2. Make the Canvas an odd width

  3. Add a slight offset to hw and hh so Flooring doesn’t affect it as much.
    hw = me.Width * 0.5 + 0.1
    hh = me.Height * 0.5 + 0.1