Jump to content

  • Log In with Google      Sign In   
  • Create Account


C# Bresenham algorithm


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 Joel Martinez   Members   -  Reputation: 337

Like
Likes
Like

Posted 15 January 2003 - 10:14 AM

Hey all, this is gonna be my first post ... I''ve been lurking here for a bit now and have started experimenting with some basic game/graphics programming. I''ll start off by saying that I specifically want to use GDI+ as opposed to MDX because I want to learn the concepts behind it all first before I move on to a powerhouse platform like MDX or C++ or whatever. Now ... I''ve implemented (ok, copied ;-) ) the C function found here [http://www.cs.helsinki.fi/group/goa/mallinnus/lines/bresenh.html] in C# ... only problem is, that it''s rather slow. I''ve got a demo that draws a line between a point, and the location of the mouse. Once it gets longer than about 100 pixels, it starts to visibly lag behind the mouse as it redraws each frame.
void Line(Graphics g, int x1, int y1, int x2, int y2, Pen pen)
{
	int dx = x2 - x1;
	int dy = y2 - y1;
	
	int y = y1;
	int eps = 0;
	Bitmap bmp = new Bitmap(1,1,g);
	bmp.SetPixel(0,0,Color.Black);
			
	for (int x=x1; x<=x2;x++)
	{
		g.DrawImage(bmp,x,y);
				
		eps+=dy;
		if ((eps*2) &rts= dx)
		{
			y++;
			eps-=dx;
		}
	}
				
}
 
The only thing I can guess is causing it to be slow is the use of a Bitmap object to draw the pixel as there''s no way to set an individual pixel using only the Graphics object. I was wondering if anyone else here had enough C#/GDI+ experience to see if there was a way to speed this up. Thanks in advance Joel Martinez http://www.codecube.net/

Sponsor:

#2 Pseudo   Members   -  Reputation: 100

Like
Likes
Like

Posted 15 January 2003 - 10:24 AM

That is because you are sort of abusing GDI+ It''s not made for lots of direct pixel access. If you want that, DirectX will be better. If you just want to draw a line, GDI+ has functions for that already...and they are fast.

#3 Joel Martinez   Members   -  Reputation: 337

Like
Likes
Like

Posted 15 January 2003 - 10:46 AM

Right ... I totally understand that ... but I didn''t want to use the built in line function (you''re right, it is very fast) because I want to understand the underlying concept behind it all before I move on to something like DirectX.

And as far as using GDI+ ... I would assume that a 2ghz machine with 512 megs of ram using GDI+ would outperform a nintendo, and I don''t think drawing a line is beyond nintendo performance. I could be wrong though ;-)

Joel Martinez
http://www.codecube.net/

#4 RomSteady   Members   -  Reputation: 248

Like
Likes
Like

Posted 15 January 2003 - 10:59 AM

Every time you call bmp.SetPixel, you aren''t just doing direct pixel access.

You are first locking the entire surface in memory. SetPixel doesn''t just lock the 1 pixel rectangle you are working on.

Then you are modifying the pixel.

Finally, you are unlocking the surface.

The lock/unlock is what is taking all of the time.

If you want to do this fast, either do it in a backbuffer then blit it to the screen, or lock the buffer once, then use direct memory access to change the pixels, then unlock the buffer.

RomSteady - Test Locally, Test Globally, Test Early, Test Often

#5 gmcbay   Members   -  Reputation: 130

Like
Likes
Like

Posted 15 January 2003 - 11:23 AM

Yeah the constant bitmap locking is killing you, as RomSteady has said.

I''d suggest you create a Bitmap equal to the size of the Graphics''s surface area and then use the Bitmap::LockBits() method to get a handle on the raw bitmap data buffer (see the GDI+ documentation for Bitmap::LockBits and BitmapData class for exact usage information), plot the pixels into that buffer, unlock and THEN DrawImage from the Bitmap to the Graphics object. If done correctly this should be _significantly_ faster than what you are doing now. Doing it this way requires an understanding of pixel formats and surface pitch vs surface width, so if you''re not already familiar with these concepts you may have some reading up to do, but the GDI+ documentation and the examples it includes should be enough to get you going.







Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS