Home Forums HCL Reviews Tutorials Articles Register Search Today's Posts Mark Forums Read
 LinuxQuestions.org C/SDL; creating my own drawLine function
 Programming This forum is for all programming questions. The question does not have to be directly related to Linux and any language is fair game.

Notices

 09-20-2002, 10:38 AM #1 TheLinuxDuck Member Contributing Member   Registered: Sep 2002 Location: Tulsa, OK Distribution: Slack, baby! Posts: 349 Rep: C/SDL; creating my own drawLine function I'm in somewhat unfamiliar territory here... I know C decent enough, but not math, and that's my problem here... (= I'm attempting to create a drawLine function using the SDL stuff (which really isn't the important point). I've already got a drawPixel function (taken from a tutorial here: http://cone3d.gamedev.net/cgi-bin/in...ls/gfxsdl/tut1 ) I've written out a drawLine function, but it's not working correctly, and I'm a little frustrated. (= FYI: struct XYcoord only contains two doubles x and y. (doubles for precision). Code: ```void drawLine(SDL_Surface *screen, const struct XYcoord *pos1, const struct XYcoord *pos2, Uint8 r, Uint8 g, Uint8 b) { struct XYcoord add, start, end; // Add contains the actual increment to each coordinate to move // from start to end. first we divide end by start to determine // a ratio of how much to move each pixel of the line add.x = end.x / start.x; add.y = end.y / start.y; // Next, we need to make sure that at least one of the add's is // 1, so that we don't write several pixels to the same place on // the screen. We do this by taking the largest value, and // divide by it's positive value (to preserve sign) if(add.x > add.y) { add.y /= abs(add.x); // should be between 0.0 and 1.0 add.x /= abs(add.x); // should be 1.0 } else { add.x /= abs(add.y); // should be between 0.0 and 1.0 add.y /= abs(add.y); // should be 1.0 } // Lastly, draw a pixel at start and increment it by add. We // test for the (int) equality, since matching two doubles in this // manner would probably never be true while((int)start.x != (int)end.x && (int)start.y != (int)end.y) { drawPixel(screen, (int)start.x, (int)start.y, r, g, b); start.x += add.x; start.y += add.y; } }``` There really isn't alot to this, but for some reason, it's not working, and my brain is telling me to stop trying to figure it out and ask for help. (= So, if anyone can help me find the problem, I'd be very happy and smile and grin and do other weird things that people do when happy.
 09-20-2002, 10:40 AM #2 TheLinuxDuck Member Contributing Member   Registered: Sep 2002 Location: Tulsa, OK Distribution: Slack, baby! Posts: 349 Original Poster Rep: Heh... I'm dumb.. I just realized my error. I never assigned any value to start or end.  That appears to only be the surface of the problems. The program segfaulted! Heh... I'll have to play and see if I can't finger out why. Still, if anyone has any suggestions, improvements, or sees something I'm doing wrong, please let me know! Danks! Last edited by TheLinuxDuck; 09-20-2002 at 10:43 AM.
 09-20-2002, 11:25 AM #3 acid_kewpie Moderator   Registered: Jun 2001 Location: UK Distribution: Gentoo, RHEL, Fedora, Centos Posts: 43,417 Rep: I don't know how much you know about this sort of thing, but in my last year at University i did a really interesting module on graphics algorithms. Have you read about Bresenham's Line algorithm? http://www.dcs.warwick.ac.uk/~tja/Ca...tml/sld021.htm really neat ideas there i think. there's further acceleration methods a little later on.
 09-20-2002, 11:29 AM #4 TheLinuxDuck Member Contributing Member   Registered: Sep 2002 Location: Tulsa, OK Distribution: Slack, baby! Posts: 349 Original Poster Rep: Thanks.. I've glanced at it.. now I wish I would have paid better attention in school... ::sigh:: )= I'll see if I can't make sense of it. (=
 09-20-2002, 11:34 AM #5 acid_kewpie Moderator   Registered: Jun 2001 Location: UK Distribution: Gentoo, RHEL, Fedora, Centos Posts: 43,417 Rep: heh, i might have finally been useful to you! cool...

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is Off HTML code is Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post pujolasdf Linux - Newbie 4 03-13-2005 08:50 AM Bassam Programming 1 06-03-2004 02:44 AM ananthbv Programming 10 05-04-2004 02:31 PM nazula Programming 1 04-19-2004 01:21 PM fatherg Programming 2 03-29-2004 04:07 PM

LinuxQuestions.org

All times are GMT -5. The time now is 11:09 PM.

 Contact Us - Advertising Info - Rules - LQ Merchandise - Donations - Contributing Member - LQ Sitemap -