1 Replies - 7348 Views - Last Post: 14 September 2008 - 10:18 PM Rate Topic: -----

#1 TepidPenguin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 29-March 08

Drawing lines between points

Post icon  Posted 14 September 2008 - 08:56 PM

What I'm trying to accomplish with this code is be able to draw the parabola in which will allow the "connecting" or drawing of lines between points on my parabola. I think that I'm getting mixed on what to say in the code so that it will connect the points and continue connecting the points to show up a solid line on the console. What I'm getting now is that it seems to be filling or drawing from the same point and "fills" the parabola instead of drawing from one point to the next. Any help would be appreciated.


/*
parabola2.cpp

Draws a parabola with horizontal axis (pg 52)
made up of line segments
*/

#include <stdio.h>
#include <windows.h>

#include "resource.h"

// your path for this include may vary
#include "GraphicsFramework.h"

// Global variable to store the graphics framwork object
GraphicsFramework* PGraphics;

HWND HOutput = 0;  // handle to the output control
HWND HDialog = 0;

// function to get the absolute value of an integer
int Abs(int x) {
	if (x < 0)  return -x;
	else		return x;
}

// function to get the sign (+1 or -1) of an integer
int Sign(int x) {
	if (x < 0)  return -1;
	else		return 1;
}

void drawLine(int xPrev, int yPrev, int x, int y)
{
	int x1 = xPrev;
	int y1 = yPrev;
	int x2 = x;
	int y2 = y;
	int dy = y2 - y1;
	int dx = x2 - x1;
	COLORREF blue = RGB(0, 0, 255);

	

	if (Abs(dy) > Abs(dx)) {
		// since there is a greater change in y than x we must
		// loop in y, calculate x and draw
		for (y=y1; y != y2; y += Sign(dy)) {
			x = x1 + (y - y1) * dx / dy;
			PGraphics->AddPoint(x, y, blue);

		}
	}
	
	else {
		// since there is a greater (or equal) change in x than y we must
		// loop in x, calculate y and draw
		for (x=x1; x != x2; x += Sign(dx)) {
			y = y1 + (x - x1) * dy / dx;
			PGraphics->AddPoint(x, y, blue);
	
		}
	}

	// draw the last pixel
	PGraphics->AddPoint(x2, y2, blue);

	// draw the points
	PGraphics->Draw();
}

void DrawStuff() {
	COLORREF green = RGB(0, 255, 0);	 // green color to draw with
	COLORREF blue = RGB(0, 0, 255);  // blue color to draw with

	char str[32];					   // string to store user input
	int h, k;						   // parabola vertex
	double a;						   // parabola constant - might be a decimal
	int x, y;						   // loop and point variables
	int xPrev, yPrev;				   // previous point for drawng line segments
	int ymin, ymax;					 // limits for y loop
	RECT rect;						  // rectangle for the output window

	// get the user input from the edit boxes and 
	// convert string input to integer
	GetDlgItemText(HDialog, IDC_EDIT_VERTEXX, str, 32);
	h = atoi(str);
	GetDlgItemText(HDialog, IDC_EDIT_VERTEXY, str, 32);
	k = atoi(str);
	GetDlgItemText(HDialog, IDC_EDIT_CONSTA, str, 32);
	a = atof(str);							  // use atof to allow user to enter a decimal

	// get the rect for this window
	GetClientRect(HOutput, &rect);

	// use the rectangle info to set up y loop limits
	ymin = -(rect.bottom - rect.top) / 2;
	ymax =  (rect.bottom - rect.top) / 2;

	// clear the scene and add an axis
	PGraphics->ClearScene(RGB(0, 0, 0));
	PGraphics->AddAxis(RGB(150, 150, 150), 10);

	// loop in y, calculate x and draw
	yPrev = ymin;
	xPrev = (int) (a * (yPrev-k)*(yPrev-k))+h;
	if (y = ymin; y <= ymax; y++) 
	{
		x = (int)( a * (y-k) * (y-k) ) + h;
		PGraphics->AddPoint(x, y, green); 
		drawLine(xPrev, yPrev, x, y);
		
	}

	  

	// draw the points
	PGraphics->Draw();
}

/*
DialogProc
this is the window event handler for the main dialog
*/
BOOL CALLBACK DialogProc (HWND hwnd, 
	UINT message, 
	WPARAM wParam, 
	LPARAM lParam)
{
	switch(message)
	{
	case WM_INITDIALOG:
		// dialog is initializing - store the picture box handle in a global variable for later
		HOutput = GetDlgItem(hwnd, IDC_PICTURE_OUTPUT);		

		// instantiate and initialize our graphics framework object
		PGraphics = new GraphicsFramework(HOutput);

		break;

	case WM_COMMAND:
		switch(LOWORD(wParam))
		{
			case IDC_BTN_DRAW:
				// draw button was pressed
				DrawStuff();
				break;
			case IDC_BTN_CLEAR:
				// clear button was pressed so clear the scene and draw the empty scene
				PGraphics->ClearScene(RGB(0, 0, 0));
				PGraphics->Draw();
				break;
			case IDCANCEL:
				// user is quitting so release the GraphicsFramework object and quit
				delete PGraphics;
				PostQuitMessage(0);
				break;
		}
				  
	}
	return FALSE;
}

// this is the main function that starts the application
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, char * cmdParam, int cmdShow)
{
	// create the main window
	// store its handle in a global if needed
	HDialog = CreateDialog (GetModuleHandle(NULL), 
		MAKEINTRESOURCE(IDD_DIALOG1), 
		0, 
		DialogProc);

	// make the dialog visible
	ShowWindow(HDialog, SW_SHOW);

	// standard windows message loop
	MSG  msg;
	int status;
	while ((status = GetMessage (&msg, 0, 0, 0)) != 0)
	{
		if (status == -1)
			return -1;
		// avoid processing messages for the dialog
		if (!IsDialogMessage (HDialog, & msg))
		{
			TranslateMessage ( & msg );
			DispatchMessage ( & msg );
		}
	}

	return (int)(msg.wParam);
}



Is This A Good Question/Topic? 0
  • +

Replies To: Drawing lines between points

#2 TepidPenguin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 29-March 08

Re: Drawing lines between points

Posted 14 September 2008 - 10:18 PM

I found out what my problem was. Thanks to all those who looked at this though. I just needed to reference xPrev = x; and yPrev = y; so that it was able to know what the next points were.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1