943,817 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 3530
  • C RSS
Jun 17th, 2008
0

Complete C Program to draw a sin curve without any application

Expand Post »
i need a complete C program to draw a sin curve without any application and the output should be displayed in dos prompt. can anyone please help me
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kamal85 is offline Offline
1 posts
since Jun 2008
Jun 17th, 2008
0

Re: Complete C Program to draw a sin curve without any application

Are you working on Windows or Unix? I know that windows has some functions in the Win32 API for setting pixels in the console screen.
Reputation Points: 69
Solved Threads: 28
Posting Whiz
Clockowl is offline Offline
376 posts
since May 2008
Jun 17th, 2008
0

Re: Complete C Program to draw a sin curve without any application

Click to Expand / Collapse  Quote originally posted by kamal85 ...
i need a complete C program to draw a sin curve without any application and the output should be displayed in dos prompt. can anyone please help me
Here's a sampleprogram that draws a sine with the SetPixel() function (as Clockowl suggested):
cpp Syntax (Toggle Plain Text)
  1. #include<windows.h>
  2. #include<iostream>
  3. #include <cmath>
  4.  
  5. int main()
  6. {
  7. HWND console = GetConsoleWindow();
  8. HDC dc = GetDC(console);
  9. int pixel =0;
  10. COLORREF C1= RGB(255,0,0); //red
  11. for (double i = 0; i< 6.3; i+=0.05)
  12. {
  13. SetPixel(dc,pixel,(int)(100+50*sin(i)),C1);
  14. pixel+=1;
  15. }
  16. ReleaseDC(console, dc);
  17. std::cin.ignore();
  18. return 0;
  19. }

I remember something about a polyline() function that can connect the dots, but that's for you to research.
Personally I would really recommend to use wxWidgets (or something like it). It has great function for drawing graphs and is platform-independent
Last edited by Nick Evan; Jun 17th, 2008 at 11:11 am.
Moderator
Featured Poster
Reputation Points: 4142
Solved Threads: 394
Industrious Poster
Nick Evan is offline Offline
4,132 posts
since Oct 2006
Jun 17th, 2008
1

Re: Complete C Program to draw a sin curve without any application

That's a C++ program btw, not C.

To make it C:

  1. #define _WIN32_WINNT 0x0500
  2.  
  3. #include <windows.h>
  4. #include <stdio.h>
  5. #include <math.h>
  6.  
  7. int main()
  8. {
  9. HWND console = GetConsoleWindow();
  10. HDC dc = GetDC(console);
  11. int pixel = 0;
  12. unsigned int loopcounter = 0;
  13. unsigned int loopmax = 126;
  14. COLORREF C1= RGB(255,0,0); //red
  15. double i = 0;
  16.  
  17. for (loopcounter = 0; loopcounter < loopmax; loopcounter++)
  18. {
  19. SetPixel(dc,pixel,(int)(100+50*sin(i)),C1);
  20. pixel++;
  21. i += 0.05;
  22. }
  23. ReleaseDC(console, dc);
  24. getchar();
  25. return 0;
  26. }

And link it with gdi32.
Reputation Points: 69
Solved Threads: 28
Posting Whiz
Clockowl is offline Offline
376 posts
since May 2008
Jun 17th, 2008
0

Re: Complete C Program to draw a sin curve without any application

Click to Expand / Collapse  Quote originally posted by Clockowl ...
That's a C++ program btw, not C.
You're absolutely right! I switch between the forums quite a bit and sometimes lose track of where I am

Click to Expand / Collapse  Quote originally posted by Clockowl ...
To make it C
Thanks for the effort.
Hoot!
Moderator
Featured Poster
Reputation Points: 4142
Solved Threads: 394
Industrious Poster
Nick Evan is offline Offline
4,132 posts
since Oct 2006

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: fread() doesn't work when 're-used' in a for( ; ;) function
Next Thread in C Forum Timeline: Length of file





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


Follow us on Twitter


© 2011 DaniWeb® LLC