Thread: Geometry-shapes functions' syntax ?

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    18

    Post Geometry-shapes functions' syntax ?

    I have been googling for most of the day to find the page which has all the shapes syntaxes listed but could not find many of them apart from line function and circle.

    I am a newbie and i was just skipping the pages which had "C++" written over it bcause i get really confused between these two languages.

    So just wondering, if anyone can direct me to some page where i can find the ANSI C (and not C++) syntax and a bit explanation about those shapes like rectangle, rhombus, trapezium, polygons etc etc

    Or otherwise, write in your own words about the syntaxes that you know.

    Thankyou very much in anticipation

  2. #2
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    There are no ANSI C functions related to geometric shapes. An ANSI C program doesn't even need a screen in order to run, so it wouldn't make much sense to have geometric shape functions. You will need to use a third party library to achieve such things.

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    18
    Thanks for reply,

    I don't exactly understand your reply. Are you saying that i can't draw shapes like Circle, rectangle etc using c language alone?

    I know function like line() and circle() and they did help me draw respective shapes.

    I am using MS Visual c as a compiler

  4. #4
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    He is saying that there are no standard (as in ANSI standard) functions which do these things. Now you could write your own (so yes, you can draw shapes with a C program), or you can use someone else's library.

  5. #5
    Registered User
    Join Date
    Dec 2005
    Posts
    18
    Alrightttt...

    I am using MS visual C++ compiler.

    What do i do to draw ,lets say, square.

    how would you draw it. Please bear with me as i do not know much about C language itself. let alone library files etc

  6. #6
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    From your last post, you appear not to understand. MS Visual C++ is a Microsoft Windows specific environment, and if you want to do things like draw squares graphically, you will need to use non-C standard libraries. I suggest you post such questions in the Windows programming forum, rather than this C forum. For further information (that won't help you draw a square), read on.

    Anything that falls under the category of graphics, beyond simple text character set based graphics will require something beyond the functions that standard C provides.

    For example, the line() and circle() functions you speak of are provided with your compiler as an addon, and you need to refer to the documentation provided with that environment. These functions (or layers below like a setpixel function) will have an understanding of the environment so that ultimately pixels can be written to video memory in the platform and hardware specific way.

    C has no concept of a pixel, or a screen, it only understands how to put text characters onto an output stream. This isn't much use for graphics. A third party library might have a function that knows what address in memory corresponds to a particular pixel on the screen. More likely, the third party library will have a function that knows how to send a pixel to a particular window or display context, and then underlying OS will take care that the window appears and other such details.

    Drawing a circle might seem like a simple concept that could easily be abstracted across many platforms, but in most environments, you will actually be drawing to a specific rectangular area such as a window, which will have to know how to be moved, resized, brought behind/in front, et cetera, rather than just a case of putting a circle onto the screen. These are all well beyond the scope of standard C.

  7. #7
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    <Simplest way I know of>

    1. Start new project
    2. Choose MFC AppWizard
    3. Choose Dialog app
    4. Once project is created, right click and choose ClassWizard.
    5. Push New class... button
    6. Select generic CWnd from the list and name your window.
    7. Go to message maps for your new window.
    8. Select WM_PAINT and click
    9. Double Click WM_PAINT code handler in bottom window
    10. Add this to your class code:

    Code:
    void CMyTestWnd::OnPaint()
    {
      CPaintDC dc(this); // device context for painting
    	
      // TODO: Add your message handler code here
      CRect rect;
      GetClientRect(&rect);
    
      dc.MoveTo(rect.left+5,rect.top+5);
    	
      dc.LineTo(rect.right-5,rect.top+5);
      dc.LineTo(rect.right-5,rect.bottom-5);
      dc.LineTo(rect.left+5,rect.bottom-5);
      dc.LineTo(rect.left+5,rect.top+5);
    
    
      // Do not call CWnd::OnPaint() for painting messages
    
    }

    Now in your Main dialog class create an instance of your window.

    Code:
    //mfc include crapola here
    
    #include "CMyTestWnd.h"
    
    
    class CMyDialog
    {
      //Mfc crapola here
    
      CMyTestWnd  MyTestWindow;
     
      //More mfc crapola here
    };
    
    //mfc crapola here
    Now right click and select class wizard. Select your main dialog class. Go to message maps.
    Select your class name from the list at left. Select WM_INITDIALOG from right and click.
    Click new function name in list at bottom.

    Inside of OnInitDialog() do this:

    Code:
    BOOL CMyDialog::OnInitDialog()
    {
      CRect rect;
      GetClientRect(&rect);
      rect.top+=5;
      rect.left+=5;
      rect.right-=5;
      rect.bottom-=5;
    
      MyTestWindow.Create(NULL,My Test Window",WS_BORDER | WS_VISIBLE,this,0);
    
      MyTestWindow.CenterWindow();
      MyTestWindow.ShowWindow(SW_SHOW);
    }

    Ok. Now run it and you will have a black square in your dialog box.

    My point here is. You don't understand this code but yet this is what it takes in an MFC program to create a window with a square in it. Or at least it's one way. You could just draw to the dialog DC but not a good idea. So you are way over your head.

    Start small, learn C/C++, buy some books on Win32, MFC, DirectX, OpenGL, etc., and then start drawing boxes.

    You don't even want me to show you the code in DirectX or pure Win32.

  8. #8
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Here is some windows code to create a simple window, and draw a square in it.
    Code:
    #include <windows.h>
    
    LONG CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
    	switch(msg)
    	{
    	case WM_CLOSE:
    		DestroyWindow(hwnd);
    		break;
    	case WM_DESTROY:
    		PostQuitMessage(0);
    		break;
    	case WM_PAINT:
    		{
    			PAINTSTRUCT		ps;
    			HDC				hdc;
    			HBRUSH			hOldBrush;
    
    			hdc = BeginPaint(hwnd,&ps);
    			/* Set a color to fill the rectangle in */
    			hOldBrush = SelectObject(hdc,GetSysColorBrush(1));
    			/* Draw the rectangle */
    			Rectangle(hdc,100,80,300,280);
    			EndPaint(hwnd,&ps);
    			SelectObject(hdc,hOldBrush);
    		}
    		break;
    	}
    	return DefWindowProc(hwnd,msg,wParam,lParam);
    }
    
    int WINAPI WinMain(HINSTANCE hInstance,  HINSTANCE hPrevInstance,  LPSTR lpCmdLine,  int nShowCmd)
    {
    	WNDCLASS	wndClass;
    	BOOL		bRet;
    	MSG			msg;
    
    	wndClass.style			= 0;
    	wndClass.lpfnWndProc	= WndProc;
    	wndClass.cbClsExtra		= 0;
    	wndClass.cbWndExtra		= 0;
    	wndClass.hInstance		= hInstance;
    	wndClass.hIcon			= LoadIcon(hInstance, IDI_APPLICATION);
    	wndClass.hCursor		= LoadCursor(NULL, IDC_ARROW);
    	wndClass.hbrBackground	= (HBRUSH)(COLOR_WINDOW + 1);
    	wndClass.lpszMenuName	= NULL;
    	wndClass.lpszClassName	= "MyClass";
    
    	if(!RegisterClass(&wndClass))
    	{
    		MessageBox(NULL,"Error registering class","Error",0);
    		return 0;
    	}
    
    	if(!CreateWindow("MyClass","My Window", WS_VISIBLE | WS_OVERLAPPEDWINDOW,
    			CW_USEDEFAULT, CW_USEDEFAULT,400,400,NULL, NULL,hInstance,0))
    	{
    		MessageBox(NULL,"Error creating window","Error",0);
    		return 0;
    	}
    
    	while((bRet = GetMessage(&msg,NULL,0,0)) != 0)
    	{
    		if(bRet == -1)
    		{
    			/* Error with GetMessage */
    			return 0;
    		}
    		else
    		{
    			TranslateMessage(&msg);
    			DispatchMessage(&msg);
    		}
    	}
    	return 0;
    }

  9. #9
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    See the Win32 version is even longer.

    Learn the language first and then learn Win32.

  10. #10
    Registered User
    Join Date
    Dec 2005
    Posts
    13
    Quote Originally Posted by bigjoke
    Alrightttt...

    I am using MS visual C++ compiler.

    What do i do to draw ,lets say, square.

    how would you draw it. Please bear with me as i do not know much about C language itself. let alone library files etc
    lets C write a procedure square(int x1,int y1,int x2,int y2)
    (x1,y1) n (x2,y2) diagonal coods

    Code:
    void square(int x1,int y1,int x2,int y2)
    {
       line(x1,y1,x2,y1);
       line(x1,y1,x1,y2);
       line(x2,y2,x1,y2);
       line(x2,y2,x2,y1);
    }

    use the above proc to draw a square....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. An array of macro functions?
    By someprogr in forum C Programming
    Replies: 6
    Last Post: 01-28-2009, 07:05 PM
  2. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 04:09 AM
  3. Replies: 7
    Last Post: 11-17-2008, 01:00 PM
  4. What's wrong with this code??
    By Xanth in forum C Programming
    Replies: 11
    Last Post: 12-23-2004, 02:41 PM
  5. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM