LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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

Reply
 
Search this Thread
Old 09-20-2002, 09:38 AM   #1
TheLinuxDuck
Member
 
Registered: Sep 2002
Location: Tulsa, OK
Distribution: Slack, baby!
Posts: 349

Rep: Reputation: 33
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.
 
Old 09-20-2002, 09:40 AM   #2
TheLinuxDuck
Member
 
Registered: Sep 2002
Location: Tulsa, OK
Distribution: Slack, baby!
Posts: 349

Original Poster
Rep: Reputation: 33
Heh... I'm dumb.. I just realized my error.
I never assigned any value to start or end.

[edit]

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 09:43 AM.
 
Old 09-20-2002, 10:25 AM   #3
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,398

Rep: Reputation: 1965Reputation: 1965Reputation: 1965Reputation: 1965Reputation: 1965Reputation: 1965Reputation: 1965Reputation: 1965Reputation: 1965Reputation: 1965Reputation: 1965
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.
 
Old 09-20-2002, 10:29 AM   #4
TheLinuxDuck
Member
 
Registered: Sep 2002
Location: Tulsa, OK
Distribution: Slack, baby!
Posts: 349

Original Poster
Rep: Reputation: 33
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.

(=
 
Old 09-20-2002, 10:34 AM   #5
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,398

Rep: Reputation: 1965Reputation: 1965Reputation: 1965Reputation: 1965Reputation: 1965Reputation: 1965Reputation: 1965Reputation: 1965Reputation: 1965Reputation: 1965Reputation: 1965
heh, i might have finally been useful to you! cool...
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Trying to program an SDL application but cannot find the SDL.h file:SuSE 9.2&KDevelop pujolasdf Linux - Newbie 4 03-13-2005 07:50 AM
what are the Hexadecimal function and ASCII function in Perl Bassam Programming 1 06-03-2004 01:44 AM
A main can be changed by a function local without passing anything to the function? ananthbv Programming 10 05-04-2004 01:31 PM
Perl exec function in linux (and system-function) nazula Programming 1 04-19-2004 12:21 PM
SDL Segementation Fault when calling function SDL_LockSurface fatherg Programming 2 03-29-2004 03:07 PM


All times are GMT -5. The time now is 07:13 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration