Page 1 of 1

how to make math / graphical plot?

Posted: Fri Jul 17, 2020 10:56 pm
by farvardin
hello,

I wanted to draw some functions using love.graphics.points

it works, I can see the individual points, but it's not persistent, a new point replace the previous one, I wanted it to draw some curves or lines, like in a graphical.

How could I achieve that?




Code: Select all

function love.load()

    x, y = 0, 0
	t=0
end

function love.update(dt)
  z=math.abs(math.sin(t)/math.cos(y+t))
  t = t +1
end
 

function love.draw()
  
	love.graphics.points(t,10+z*44)
	
end

Re: how to make math / graphical plot?

Posted: Fri Jul 17, 2020 11:26 pm
by ReFreezed
You may want to replace the default love.run as it's calling love.graphics.clear every frame.

Another way is to simply save each new point you want to draw in an array and then draw all available points every update.

(This is assuming you want to see new points appear over time.)

Re: how to make math / graphical plot?

Posted: Sat Jul 18, 2020 10:14 am
by pgimeno
Well, the obvious way is to draw the plot to a canvas.

Code: Select all

local canvas, x, y, t

function love.load()
  x, y = 0, 0
  t = 0
  canvas = love.graphics.newCanvas()
end

function love.update(dt)
  love.graphics.setCanvas(canvas)
  local z = math.abs(math.sin(t)/math.cos(y+t))
  love.graphics.rectangle("fill", t, 10 + z*44, 1, 1)
  t = t + 1
  love.graphics.setCanvas()
end
 

function love.draw()
  love.graphics.draw(canvas)
end
Edit:
ReFreezed wrote: Fri Jul 17, 2020 11:26 pm You may want to replace the default love.run as it's calling love.graphics.clear every frame.
Don't do that. There are good reasons for that to happen. Typically love.graphics.present uses double (or triple) buffering, and if you don't completely overwrite each buffer before it's drawn to the screen, the results are undefined. Furthermore, due to this, you may have half the pixels drawn to one of the buffers and the other half drawn to the other; and that's assuming that it's a double buffer and not a triple buffer.

Re: how to make math / graphical plot?

Posted: Sat Jul 18, 2020 1:21 pm
by farvardin
@ReFreezed
You may want to replace the default love.run as it's calling love.graphics.clear every frame.
thanks a lot, it's working perfectly, I've just commented the "love.graphics.clear(love.graphics.getBackgroundColor())" part!

@pgimeno
Well, the obvious way is to draw the plot to a canvas.
thank you too, it's working fine this way too. And it's probably less "hackish".

Re: how to make math / graphical plot?

Posted: Sat Jul 18, 2020 6:31 pm
by ReFreezed
pgimeno wrote: Sat Jul 18, 2020 10:14 am Well, the obvious way is to draw the plot to a canvas.
Note that canvases will still be cleared if the window is resized. The safest way is to either draw each point every frame or to use an ImageData object.

Re: how to make math / graphical plot?

Posted: Sun Jul 19, 2020 10:29 am
by pgimeno
ReFreezed wrote: Sat Jul 18, 2020 6:31 pm Note that canvases will still be cleared if the window is resized.
Cleared? No, not for me at least. The example I posted above works fine after resizing.

The canvas will no longer be the same size as the screen, but that's a different problem that can be easily solved, if you want to support resizing:

Code: Select all

function love.resize(w, h)
  local oldcanvas = canvas
  canvas = love.graphics.newCanvas(w, h)
  love.graphics.setCanvas(canvas)
  love.graphics.draw(oldcanvas)
  love.graphics.setCanvas()
  oldcanvas:release()
end
This has the problem that if the plot exceeded the screen limit, there will be a part that is blank. If you want to solve that, you can just create the initial canvas with the maximum size supported and forget about resizing, e.g.

Code: Select all

  local size = love.graphics.getSystemLimits().texturesize
  canvas = love.graphics.newCanvas(size, size)

Re: how to make math / graphical plot?

Posted: Sun Jul 19, 2020 11:09 am
by ReFreezed
pgimeno wrote: Sun Jul 19, 2020 10:29 am
ReFreezed wrote: Sat Jul 18, 2020 6:31 pm Note that canvases will still be cleared if the window is resized.
Cleared? No, not for me at least. The example I posted above works fine after resizing.
It's what the description for love.window.setMode says.
Wiki wrote:Changing the display mode may have side effects: for example, canvases will be cleared and values sent to shaders with Shader:send will be erased. Make sure to save the contents of canvases beforehand or re-draw to them afterward if you need to.
I guess I should have said "may" instead of "will", unless the wiki is plainly wrong. Canvases don't get cleared in my tests either. *shrug*

Re: how to make math / graphical plot?

Posted: Sun Jul 19, 2020 11:04 pm
by pgimeno
Oh, you meant resizing using setMode? I tried resizing using the window handles (after setting c.window.resizable = true).

Re: how to make math / graphical plot?

Posted: Mon Jul 20, 2020 3:16 am
by ReFreezed
Well, is there a difference? The only way to resize the window through code is setMode and updateMode. Maybe it is that canvases won't ever get cleared if the window size is the only property that has changed when calling setMode/updateMode, and that dragging borders to resize is actually safe too. I'm just wildly speculating here. :?