how to make math / graphical plot?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
farvardin
Party member
Posts: 167
Joined: Sat Jun 28, 2008 6:46 pm

how to make math / graphical plot?

Post 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
Attachments
math_plot.love
(299 Bytes) Downloaded 172 times
User avatar
ReFreezed
Party member
Posts: 612
Joined: Sun Oct 25, 2015 11:32 pm
Location: Sweden
Contact:

Re: how to make math / graphical plot?

Post 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.)
Tools: Hot Particles, LuaPreprocess, InputField, (more) Games: Momento Temporis
"If each mistake being made is a new one, then progress is being made."
User avatar
pgimeno
Party member
Posts: 3656
Joined: Sun Oct 18, 2015 2:58 pm

Re: how to make math / graphical plot?

Post 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.
User avatar
farvardin
Party member
Posts: 167
Joined: Sat Jun 28, 2008 6:46 pm

Re: how to make math / graphical plot?

Post 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".
User avatar
ReFreezed
Party member
Posts: 612
Joined: Sun Oct 25, 2015 11:32 pm
Location: Sweden
Contact:

Re: how to make math / graphical plot?

Post 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.
Tools: Hot Particles, LuaPreprocess, InputField, (more) Games: Momento Temporis
"If each mistake being made is a new one, then progress is being made."
User avatar
pgimeno
Party member
Posts: 3656
Joined: Sun Oct 18, 2015 2:58 pm

Re: how to make math / graphical plot?

Post 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)
User avatar
ReFreezed
Party member
Posts: 612
Joined: Sun Oct 25, 2015 11:32 pm
Location: Sweden
Contact:

Re: how to make math / graphical plot?

Post 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*
Tools: Hot Particles, LuaPreprocess, InputField, (more) Games: Momento Temporis
"If each mistake being made is a new one, then progress is being made."
User avatar
pgimeno
Party member
Posts: 3656
Joined: Sun Oct 18, 2015 2:58 pm

Re: how to make math / graphical plot?

Post by pgimeno »

Oh, you meant resizing using setMode? I tried resizing using the window handles (after setting c.window.resizable = true).
User avatar
ReFreezed
Party member
Posts: 612
Joined: Sun Oct 25, 2015 11:32 pm
Location: Sweden
Contact:

Re: how to make math / graphical plot?

Post 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. :?
Tools: Hot Particles, LuaPreprocess, InputField, (more) Games: Momento Temporis
"If each mistake being made is a new one, then progress is being made."
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 5 guests