Complete C Program to draw a sin curve without any application

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jun 2008
Posts: 1
Reputation: kamal85 is an unknown quantity at this point 
Solved Threads: 0
kamal85 kamal85 is offline Offline
Newbie Poster

Complete C Program to draw a sin curve without any application

 
0
  #1
Jun 17th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 376
Reputation: Clockowl is on a distinguished road 
Solved Threads: 27
Clockowl's Avatar
Clockowl Clockowl is offline Offline
Posting Whiz

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

 
0
  #2
Jun 17th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,971
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 308
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is online now Online
Cenosillicaphobiac

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

 
0
  #3
Jun 17th, 2008
Originally Posted by kamal85 View 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
Here's a sampleprogram that draws a sine with the SetPixel() function (as Clockowl suggested):
  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 niek_e; Jun 17th, 2008 at 11:11 am.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 376
Reputation: Clockowl is on a distinguished road 
Solved Threads: 27
Clockowl's Avatar
Clockowl Clockowl is offline Offline
Posting Whiz

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

 
1
  #4
Jun 17th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,971
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 308
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is online now Online
Cenosillicaphobiac

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

 
0
  #5
Jun 17th, 2008
Originally Posted by Clockowl View Post
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

Originally Posted by Clockowl View Post
To make it C
Thanks for the effort.
Hoot!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC