Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Artifacts left on screen on plotting program  (Read 1868 times)

0 Members and 1 Guest are viewing this topic.

Davitosan

  • Newbie
  • *
  • Posts: 5
    • View Profile
Artifacts left on screen on plotting program
« on: November 22, 2014, 01:38:26 am »
Hello! I'm currently studying Fourier Series, and our teacher asked us to develop a program that plots one. I had dabbled in SFML before and I thought it would be as good as any other platform for this task.

Without getting too much into it, the function depends on three variables, 'L', 'a' and 't'. I wanted to add three sliders beneath the graph so the user could change the value of each variable in real time. After a few hours, I got everything up and running, but there's a weird problem. The first slider, the one that controls the variable 'L', leaves artifacts on the screen when the plot becomes smaller. This doesn't happen when the graph gets bigger, or with any of the other two variables. I've tried to figure out the reason for this behavior, but I'm stumped.

I've attached pictures of this happening, and the code for the program, which can also be found here: http://pastebin.com/dUPm93rr

I've also attached the compiled file, fourier.bin.

The forum didn't allow me to attach more images, so I couldn't add the png's for the grid and the sliders, but if anyone wishes to compile this, I'll gladly share them.

Relevant data:

SFML 2.0 for linux, downloaded and installed from the ubuntu repositories.
Compiled with g++ in lubuntu

Any help will be truly appreciated.

(I apologize beforehand, I'm not a proficient programmer and the code is a little messy)

Ruckamongus

  • Jr. Member
  • **
  • Posts: 70
    • View Profile
Re: Artifacts left on screen on plotting program
« Reply #1 on: November 22, 2014, 01:58:49 am »
Davitosan, you're code isn't bad; the mistake (I believe) is a very simple oversight. The sf::VertexArray isn't being cleared. So As you fill up the vertex array it gets bigger, but when you update the vertices to a smaller number than what was there previously (by making L less) it doesn't remove the unused vertices at the end. Simplying adding "points.clear()" at line 196 should fix your issue!

By the way, people of the forum are going to complain about posting a minimal example. Next time you should try to minimize the code you post; it's difficult in the case though.

Davitosan

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Artifacts left on screen on plotting program
« Reply #2 on: November 22, 2014, 02:21:57 am »
Ruckamongus, thanks a lot for your answer!

I tried adding points.clear() where you said, but that made the plot disappear entirely. I also tried adding it after the window.display() line, but the result was the same (no plot).

I think the problem is that the array gets created and populated before the while loop (line 172). I moved that line inside the loop, and the points.clear() after window.display(), and now the program runs as expected!

I don't like the idea of creating the entire array at every iteration of the while loop, though. It doesn't seem right, and probably there's a better way of doing this. Any thoughts?

Ruckamongus

  • Jr. Member
  • **
  • Posts: 70
    • View Profile
Re: Artifacts left on screen on plotting program
« Reply #3 on: November 22, 2014, 02:34:58 am »
Ah right, bad logic on my end! Glad you got it figured out though. I can understand why you'd want to avoid clearing it every time, so you have a couple of options:
  • Allocate the memory at the beginning of the while loop so the allocation happens once (the vector will not have to grow repeatedly).
  • Write your own vertex array class.
A vertex array is just an std::vector<sf::Vertex>. You could write a nearly identical class to SFML's, but keep track of the actual "working" number of vertices. This way you can still use a growing vector, but when you write the overloaded draw() function, you will only draw up to the "working" size. This is probably the best approach if you're comfortable with it. Look at the source of sf::VertexArray and you can copy and paste almost all of it. Just add a member variable for working number of vertices and slightly adapt the draw function. For your convenience: https://github.com/SFML/SFML/blob/master/src/SFML/Graphics/VertexArray.cpp

Hapax

  • Hero Member
  • *****
  • Posts: 3351
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Artifacts left on screen on plotting program
« Reply #4 on: November 22, 2014, 02:59:01 am »
You can resize the vertex array using points.resize(app.L).
This will remove any extra points, add points if there are not enough, and not do anything if it's already the correct size - just like a normal std::vector  ;)
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

 

anything