Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

plotting/graphing points and areas 2

Status
Not open for further replies.

Sedai

Programmer
Mar 4, 2001
158
US
Hi!

I need to be able to draw points, given X and Y coords, then connect them, and then 'fill up' an area defined by three or more lines.
I have the algorith for connecting 3 and 4 points and calculating the area of the figure, given my rules.
How can I show this graphically?
any special .h file with predefined functions...?
thanks
Avendeval


 
Hi,
use the device context functions. try something like this...
...
CDC *pDC; //device context for drawing graph
pDC = GetDC();

CPen blackpen; //create a pen
blackpen.CreatePen( PS_SOLID, 1, RGB(0,0,0) );//black
pDC->SelectObject( &blackpen ); //select it

pDC->MoveTo(25,250);// move somwhere
pDC->LineTo(550,250); //draw line from previous pos
//to this pos
...
hope this helps. good luck
-=$umedha=-
 
THANKS A LOT, but what header file do i need to #inlcude? Avendeval


 
#define WIN32_LEAN_AND_MEAN
#include <windows.h>

#define WIN_NAME &quot;Triangle Fill&quot;
#define WIN_X 200
#define WIN_Y 200
#define WINPOS_X 20
#define WINPOS_Y 90
#define NUM_OF_POINTS 3

struct TriInfo
{
int iNumOfPoints;
POINT Points[NUM_OF_POINTS];
} triInfo;

void Clear_Tri( TriInfo& tri );
void Set_Tri_Info( const HWND& hWnd,TriInfo& tri,int x,int y);
void Close_Tri( const HWND& hWnd,TriInfo& tri );
void Fill_Tri( const HWND& hWnd,TriInfo& tri );
LRESULT CALLBACK WindowProc( HWND hwnd,
UINT msg,
WPARAM wparam,
LPARAM lparam );

int WINAPI WinMain( HINSTANCE hinstance,
HINSTANCE hprevinstance,
LPSTR lpcmdline,
int ncmdshow)
{

HWND hWnd;
MSG mSg;
WNDCLASSEX winClass;

winClass.cbSize = sizeof(WNDCLASSEX);
winClass.style = CS_DBLCLKS | CS_OWNDC |
CS_HREDRAW | CS_VREDRAW;
winClass.lpfnWndProc = WindowProc;
winClass.cbClsExtra = 0;
winClass.cbWndExtra = 0;
winClass.hInstance = hinstance;
winClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
winClass.hCursor = LoadCursor(NULL, IDC_ARROW);
winClass.hbrBackground= (HBRUSH) GetStockObject(BLACK_BRUSH);
winClass.lpszMenuName = NULL;
winClass.lpszClassName= &quot;WIN_SHELL&quot;;
winClass.hIconSm = NULL;


if( !RegisterClassEx( &winClass ) ) return FALSE;

if( !(hWnd = CreateWindow(&quot;WIN_SHELL&quot;,
WIN_NAME,
WS_VISIBLE | WS_CAPTION |
WS_MINIMIZEBOX | WS_SYSMENU,
WINPOS_X,WINPOS_Y,
WIN_X,WIN_Y,
NULL,
NULL,
hinstance,
NULL)))
return FALSE;

while ( true )
{
if( PeekMessage( &mSg,NULL,0,0,PM_REMOVE ) )
{
if( mSg.message == WM_QUIT )
break;

TranslateMessage( &mSg );
DispatchMessage ( &mSg );
}
}

return ( mSg.wParam );

}


LRESULT CALLBACK WindowProc( HWND hWnd,
UINT mSg,
WPARAM wParam,
LPARAM lParam )
{

PAINTSTRUCT ps;
HDC hdc;
int iCursorX,iCursorY;

switch ( mSg )
{
case WM_CREATE:

Clear_Tri( triInfo );

return 0;

case WM_PAINT:

hdc = BeginPaint( hWnd,&ps);
EndPaint( hWnd,&ps );

return 0;

case WM_DESTROY:
PostQuitMessage( 0 );
return 0;

case WM_LBUTTONDOWN:

iCursorX = ( int )LOWORD( lParam );
iCursorY = ( int )HIWORD( lParam );
hdc = GetDC( hWnd );

SetPixel( hdc,iCursorX,iCursorY,RGB( 255,0,0 ) );
Set_Tri_Info( hWnd,triInfo,iCursorX,iCursorY );

ReleaseDC( hWnd,hdc );

return 0;

case WM_RBUTTONDOWN:
MessageBox( hWnd,&quot;#include <LiquidBinary>&quot;,&quot;By Mike L.G.&quot;,MB_OK );
return 0;

default:
break;
}

return ( DefWindowProc( hWnd, mSg, wParam, lParam ) );

}

void Set_Tri_Info( const HWND& hWnd,TriInfo& tri,int iX,int iY )
{
int iPoint = tri.iNumOfPoints;
tri.Points[iPoint].x = iX;
tri.Points[iPoint].y = iY;

if ( ++tri.iNumOfPoints == NUM_OF_POINTS )
{
Close_Tri( hWnd,tri );
for ( int i=0;i<NUM_OF_POINTS;i++ )
{
tri.Points.x = 0;
tri.Points.y = 0;
}
tri.iNumOfPoints = 0;
}

}

void Close_Tri( const HWND& hWnd,TriInfo& tri )
{
HDC hdc = GetDC( hWnd );
HPEN hRedPen = CreatePen( PS_SOLID,0,RGB( 255,0,0 ) );
SelectObject( hdc,hRedPen );

MoveToEx( hdc,tri.Points[0].x,tri.Points[0].y,NULL );
LineTo( hdc,tri.Points[1].x,tri.Points[1].y );
LineTo( hdc,tri.Points[2].x,tri.Points[2].y );
LineTo( hdc,tri.Points[0].x,tri.Points[0].y );
Fill_Tri( hWnd,tri );

ReleaseDC( hWnd,hdc );
DeleteObject( hRedPen );
}

void Fill_Tri( const HWND& hWnd,TriInfo& tri )
{

HDC hdc = GetDC( hWnd );
HBRUSH hRedBrush = CreateSolidBrush( RGB( 255,0,0 ) );
HRGN triReg;

triReg = CreatePolygonRgn( tri.Points ,NUM_OF_POINTS,ALTERNATE );
FillRgn( hdc,triReg,hRedBrush );
ReleaseDC( hWnd,hdc );
DeleteObject( hRedBrush );

}

void Clear_Tri( TriInfo& tri )
{
for ( int i=0;i<NUM_OF_POINTS;i++ )
{
tri.Points.x = 0;
tri.Points.y = 0;
}
tri.iNumOfPoints = 0;
}


The above source code allows the user to plot 3 points with each left click of the mouse. The points will be drawn as a single red pixel on the windows client area. When the 3rd point is plotted, a function is called to draw the outside of the triangle, then another function is called to fill in the triangle with a red brush. If you so choose,you could also implement a rectangle option in this short program. I coded the above source using just the raw win32 API to create a generic window and a few GDI calls for the line drawing and filling of the triangles. All of the API and GDI calls are well documented in the MSDN as well as on the net. To build the source, just copy and paste it to a .cpp file,create a new win32 app project in VC++, and compile. I do hope this helps you out a little.


Mike L.G.
mlg400@blazemail.com
 
actually i was thinking of a console applicatin when i asked the questions. probably ill end up doing it as a windows app, but still, can u tell me if there is a way to draw/plot in a DOS console?
thanks. Avendeval


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top