Jump to content

Graphics programming project - help appreciated

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
4 replies to this topic

#1
alexbabwa

alexbabwa

    Newbie

  • Members
  • Pip
  • 6 posts
Hello everyone :) for my final year assignment i have decided to make a maze with an animation of a few mice running its course. I have made the maze ok however when i make the mouse its not drawn how it should be. So i checked the coordinates about 10 times, the lines and just about the whole program.

The program runs, but the mouse is not drawn according to the points and lines i gave it. Sadly i am completely stuck. Realx/y are all related to the maze and works perfectly. Realx/ym1 is related to the mouse. The first sub routine is converting the coordinates to be displayed on a 18 by 18 canvas.

If any of you could shed some light i would be very grateful.


#include <conio.h>

#include <graphics.h>

#include <math.h>

#include <dos.h>

#include <stdio.h>

#include <iostream.h>


/*Define global variables*/


	const int np = 274, nl = 137;		/* np - number of points. */	/* nl - number of lines. */

	const int npm1 = 6, nlm1 = 6;

	const int transanp = 10, transbnp = 10;

	float realx[np] = {2, 13, 0, 2, 3, 7, 8, 12, 13, 17, 0, 1, 3, 5, 9, 11, 13, 14, 16, 17, 2, 4, 5, 7, 10, 12, 14, 15, 16, 18, 5, 7, 8, 9, 11, 12, 13, 15, 17, 18, 2, 4, 5, 7, 9, 10, 11, 16, 1, 2, 4, 8, 9, 13, 14, 16, 1, 2, 11, 12, 14, 16, 1, 3, 4, 5, 14, 17, 1, 3, 6, 7, 8, 9, 10, 11, 12, 13, 14, 16, 1, 2, 3, 5, 6, 7, 8, 15, 4, 5, 6, 10, 13, 15, 2, 3, 5, 7, 8, 12, 14, 16, 4, 7, 8, 11, 16, 17, 4, 10, 12, 14, 15, 16, 4, 5, 7, 9, 11, 14, 4, 5, 7, 13, 14, 15, 1, 3, 4, 5, 6, 9, 2, 4,	1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 11, 12, 12, 12, 12, 12, 12,	12, 12, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 15, 15, 15, 15, 15, 15, 16, 16, 16, 16, 16, 16, 17, 17, 17, 17, 17, 17};

	float realy[np] = {18, 18, 17, 17, 17, 17, 17, 17, 17, 17, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 13, 13, 13, 13, 13, 13, 13, 13, 12, 12, 12, 12, 12, 12, 12, 12, 11, 11, 11, 11, 11, 11, 10, 10, 10, 10, 10, 10, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 8, 8, 8, 8, 8, 8, 8, 8, 7, 7, 7, 7, 7, 7, 6, 6, 6, 6, 6, 6, 6, 6, 5, 5, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 0, 0, 17, 12, 11, 10, 9, 8, 1, 0, 18, 16, 15, 13, 12, 11, 8, 6, 17, 16, 13, 11, 10, 9, 6, 1, 15, 13, 12, 11, 10, 8, 7, 5, 3, 2, 1, 0, 16, 15, 14, 7, 3, 2, 1, 0, 11, 10, 9, 7, 1, 0,	17, 15, 14, 13, 11, 9, 8, 7, 6, 5, 3, 2, 17, 15, 14, 12, 11, 9, 8, 7, 6, 5, 16, 14, 13,	12, 9, 8, 1, 0, 16, 13, 12, 9, 4, 0, 14, 13, 11, 9, 8, 6, 5, 3, 16, 15, 14, 13, 11, 9, 6, 4, 18, 17, 16, 15, 12, 9, 7, 5, 2, 0, 15, 14, 12, 11, 10, 9, 6, 4, 2, 0, 17, 14, 8, 7, 4, 2, 16, 11, 9, 5, 4, 0, 17, 15, 14, 10, 5, 0};

	int linebegin[nl] = {0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98, 100, 102, 104, 106, 108, 110, 112, 114, 116, 118, 120, 122, 124, 126, 128, 130, 132, 134, 136, 138, 140, 142, 144, 146, 148, 150, 152, 154, 156, 158, 160, 162, 164, 166, 168, 170, 172, 174, 176, 178, 180, 182, 184, 186, 188, 190, 192, 194, 196, 198, 200, 202, 204, 206, 208, 210, 212, 214, 216, 218, 220, 222, 224, 226, 228, 230, 232, 234, 236, 238, 240, 242, 244, 246, 248, 250, 252, 254, 256, 258, 260, 262, 264, 266, 268, 270, 272};

	int lineend[nl] = {1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49, 51, 53, 55, 57, 59, 61, 63, 65, 67, 69, 71, 73, 75, 77, 79, 81, 83, 85, 87, 89, 91, 93, 95, 97, 99, 101, 103, 105, 107, 109, 111, 113, 115, 117, 119, 121, 123, 125, 127, 129, 131, 133, 135, 137, 139, 141, 143, 145, 147, 149, 151, 153, 155, 157, 159, 161, 163, 165, 167, 169, 171, 173, 175, 177, 179, 181, 183, 185, 187, 189, 191, 193, 195, 197, 199, 201, 203, 205, 207, 209, 211, 213, 215, 217, 219, 221, 223, 225, 227, 229, 231, 233, 235, 237, 239, 241, 243, 245, 247, 249, 251, 253, 255, 257, 259, 261, 263, 265, 267, 269, 271, 273};

	float realxm1[npm1] = {0.3, 0.1, 0.1, 0.3, 1, 0.3};                 /*Initial points for mouse*/

	float realym1[npm1] = {16.1, 16.2, 16.3, 16.4, 16.3, 16.2};

	int linebeginm1[nlm1] = {0, 1, 2, 3, 4, 5};                            /*Lines for mouse*/

	int lineendm1[nlm1] = {1, 2, 3, 4, 5, 2};

	float a[transanp] = {1, 0, 1, 1, 1, 0, 1, 1, 1, 1};                   /*Array for translation*/

	float b[transbnp] = {0, 1, 0, 0, 0, 1, 0, 0, 0, 0};


void draw(int pcolour, int lcolour, int lstyle, int lwidth)

{

	int i, screenx[np], screeny[np];

	float screenxm1[npm1], screenym1[npm1];


	setcolor(pcolour);

	setlinestyle(lstyle,EMPTY_FILL,lwidth);

	for (i=0; i<np; i++)

	{

		screenx[i] = (realx[i])/18*639;                     /*Conversion of real coords to screen coords (18 by 18)*/

		screeny[i] = 479-(realy[i])/18*479;

	}


	for (i=0; i<npm1; i++)

	{

		screenxm1[i] = (realxm1[i])/18*639;                  /*Conversion of mouse coords*/

		screenym1[i] = 479-(realym1[i])/18*479;

	}

	setcolor(lcolour);

	for (i=0; i<nl; i++)

	{

		line(screenx[linebegin[i]],screeny[linebegin[i]],screenx[lineend[i]],screeny[lineend[i]]);

	}

	for (i=0; i<nlm1; i++)

	{

		line(screenxm1[linebegin[i]],screenym1[linebegin[i]],screenxm1[lineend[i]],screenym1[lineend[i]]);

	}

}


void trans(float xt, float yt)	/* Translation routine. */

{

	int i;

	for (i=0; i<npm1; i++)	/* Transform (translate) the object. */

	{

		realxm1[i] = realxm1[i] + xt;

		realym1[i] = realym1[i] + yt;

	}

}


int main(void)

{

	int graphdriver=DETECT, graphmode;

	initgraph(&graphdriver, &graphmode, "");

	


	draw(WHITE, RED, SOLID_LINE, NORM_WIDTH);


	int i;

	float a[transanp], b[transbnp];


	for (i=0; i>10; i++)

	{

		delay(100);

		cleardevice();

		trans(a[i],b[i]);

		draw(WHITE, RED, SOLID_LINE, NORM_WIDTH);

	 }


	getch();

	closegraph();

	return 0;

}

Edited by WingedPanther, 23 January 2010 - 08:58 AM.
Adding more comments


#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Since conio, and some of the other libraries you are using are non-standard, and I'm on Linux, I can't debug it. That said, have you verified
1) the inputs your draw function is receiving?
2) what, exactly, it is doing?
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
alexbabwa

alexbabwa

    Newbie

  • Members
  • Pip
  • 6 posts
Sorry i must not have explained it clearly. I have debugged it using TCLite but as i said the program works, there are no bugs on the report. The problem is it is not working as intended.

A mouse using the coordinates and lines (commented towards the beginning of the code) i have written should be drawn in the top left of the screen. Unfortunately after the first line is drawn successfully it all goes wrong.

The raw coordinates get converted to screen coordinates in the draw sub routine. They are then linked together with lines using linebegin and lineend. Later they are drawn by calling on the draw subroutine.

draw(WHITE, RED, SOLID_LINE, NORM_WIDTH);

That should be it, only the mouse is not drawn according to the coordinates i gave it. I thought perhaps as the array was declared as an integer it might not be able to handle decimal values. However once i changed the declarations to floats, im left with the same problem.

Sorry i cant offer more information. Im not trying to debug this program as there doesnt seem to be any. I am just trying to get it working as intended.

#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Have you tried playing with the signs on your values?
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#5
alexbabwa

alexbabwa

    Newbie

  • Members
  • Pip
  • 6 posts
Yes. I even changed to absolutes, putting 16.3 instead of x+2. I figured it out. In the second half of the draw sub routine i used 'linebegin' instead of 'linebeginm1'. So the points were correct, but it was drawing the lines using what i told it for the maze and not the mouse. Feels good to find the problem, of course now i am left with another one. I made a small for loop in the main body of the code.

for (i=0; i>10; i++)
{
delay(100);
cleardevice();
trans(a[i],b[i]);
draw(WHITE, RED, SOLID_LINE, NORM_WIDTH);
}

the error is in the trans. Im a little clueless how to call on the two arrays i made with the rest of the global declarations. Clearly a[i],b[i] is wrong. Any ideas?