943,852 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 984
  • C++ RSS
Apr 1st, 2009
0

Iteration through my vector of pixels.

Expand Post »
Hello there!

I'm having problem with my drawing graphics programming, let's say I want to draw a circle.

This circle is made out of smaller pieces called pixels.

Okay I got a pixelclass containing three values x, y and color.
My circle contains a vector which contains all the pixels that the circle is made of.

If I make a circle with a radius of 100, I get to draw 31417 pixels. If I draw all these ones as often as my program allows, my program only max out ~97fps.

I wonder is it supposed to be this slow to loop 31 thousand times and drawing one pixel each time? Am I storing these pixels the wrong way? Is vector a bad choice, what other type of storing structure would I use instead?

Thanks for any help.
// ixuz
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ixuz is offline Offline
4 posts
since Apr 2009
Apr 1st, 2009
0

Re: Iteration through my vector of pixels.

Oy! Please don't use a vector (or any other STL container) to hold individual pixel data -- Just draw directly to the target surface.

Also, you needn't directly calculate every pixel in the circle. One quarter of them is sufficient. (That is, calculate 1/4 of the circle, and draw four pixels each step.)

Good luck!
Featured Poster
Reputation Points: 1140
Solved Threads: 229
Postaholic
Duoas is offline Offline
2,039 posts
since Oct 2007
Apr 1st, 2009
0

Re: Iteration through my vector of pixels.

Okay thanks alot, I removed this vector holding pixels thing and I understand what you mean by doing more than one pixel each loop, the thought that came to my mind when you told me this was:

When I'm going to draw a line of pixels, shouldn't I use a for loop? Or is it faster by just repeating the drawPixel function 4 times in the source code?

thanks!
// ixuz

Edit: As soon as I started to program I realized what you ment by draw 4 pixels at the same time. I'm using pythagoras algorithm to calculate the distance to each pixel of the circle, to see if it's inside or outside the circles radius.

I'm drawing one circle with a radius of 150 using the graphic library SDL.
Your tips actually boosted up my drawing speed from 82circles per second to 119 circles per second thanks alot!

Is pythagoras a good or bad choice of algorithm? Is there any algorithms that are faster? Tips and tricks are welcome aswell!

Thanks in advance.
// ixuz
Last edited by ixuz; Apr 1st, 2009 at 7:42 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ixuz is offline Offline
4 posts
since Apr 2009
Apr 2nd, 2009
0

Re: Iteration through my vector of pixels.

Featured Poster
Reputation Points: 1140
Solved Threads: 229
Postaholic
Duoas is offline Offline
2,039 posts
since Oct 2007
Apr 2nd, 2009
0

Re: Iteration through my vector of pixels.

Nice link! Any other recommendations per chance?
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005
Apr 3rd, 2009
1

Re: Iteration through my vector of pixels.

Alas, I just googled it.

Try searching for things like "bresenham line circle ellipse algorithm".

Speed comes from doing the least to draw individual pixels. Hence, a lot of optimization comes from low-level accesses. In modern GUI architectures, often the case is for pixel access to occur in memory (in-memory bitmaps/pixmaps and the like), and then have the WM/OS/whatever copy the image to the graphics hardware. When you move to more direct accesses (like DirectX, etc.) then you must minimize accesses to the hardware yourself -- since it is slower than normal memory accesses.

Hope this helps.
Featured Poster
Reputation Points: 1140
Solved Threads: 229
Postaholic
Duoas is offline Offline
2,039 posts
since Oct 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: void pointer problem
Next Thread in C++ Forum Timeline: Basic C++ program assitance





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC