Cant display lines!!!

Hi! I'm trying to write a code that displays lines between user designated points with circles at each point, but I can't get the lines to display! I'm at a loss on what to do and need fresh eyes on the subject.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <iostream>
#include "graph1.h"

using namespace std;

//Function Prototypes Follow
void getNoPoints(int* no_points);
void getPoints(int* x, int* y,int no_points);
void drawPolyLine(int* x, int* y, int no_points,int objects[]);

int main()
{
  //Variable Declaration/Initialization

  int no_points = 0;
  const int MAX_POINTS = 10;
  int x[MAX_POINTS];
  int y[MAX_POINTS];
  int no_circles = 0;
  int objects[MAX_POINTS];
  
  //Display Graphics Window
  displayGraphics();

  //Get the number of points (pass the address of no_points )
  getNoPoints(&no_points);

  //Get the data for the points
  getPoints(x,y,no_points);

  //Draw the polyline
  drawPolyLine(x,y,no_points,objects);

  return 0;
}

//Function Implementation Follows
void getNoPoints(int* no_points)
{
	cout<<"Enter # of points: ";
	cin>>*no_points;
}
void getPoints(int* x, int* y,int no_points)
{
	//Variable Declaration/Initiliation
	int i=0;

	for(i=0;i<no_points;i++)
	{
	cout<<"Enter x/y coord for point #"<<(i+1)<<": ";
	cin>>x[i]>>y[i];
	}
}

void drawPolyLine(int* x, int* y, int no_points,int objects[])
{
	int i=0;
	
	for (i = 0; i < no_points;)
	{
	objects[i] = drawLine(x[i],y[i],x[++i],y[++i],1);
    setColor(objects[i],255,255,0);
	}

  //Display each circle

  for (i = 0; i < no_points; i++)
  {
    objects[i] = drawCircle(5,x[i],y[i]);
	setColor(objects[i],255,0,0);
  }
}






I imagine the error is in this segment:

1
2
3
4
5
6
7
int i=0;
	
	for (i = 0; i < no_points;)
	{
	objects[i] = drawLine(x[i],y[i],x[++i],y[++i],1);
    setColor(objects[i],255,255,0);
	}
Last edited on
Hi,

First up, please use code tags. Edit your post, select all the code, then press the <> button on the format menu.

Also some indentation would be good.

1
2
3
for (i = 0; i < no_points;)
{
objects[i] = drawLine(x[i],y[i],x[++i],y[++i],1);


That looks like undefined behaviour to me, because of the multiple ++i in one statement.

Use another variable which is the next point, and provide that as an argument.
Sorry for all the issues, I'm new here. But as for the code, I'm not sure I understand what you mean by using another variable as the next point?
Hi,

Well, you have a drawLine function which takes x and y coordinates. Assign the values you want to variables NextX, NextY say, then call your function with those variables:

objects[i] = drawLine(ThisX, ThisY, NextX, NextY,1);

Please update your post so it uses code tags.
I have the variable I as a user input coordinate so that when the program asks for coordinates for point 1,point 2,etc., it starts at point one, then the i++ goes on to the next point(2) and so on(like with the draw circles function). I can't just assign the variables values because it's user defined values,unless I'm not understanding what you mean?Also,if I set next x and next y to 0 then how would I code the program to have it go on to different variables until it came to 6,and the user doesn't even have to use six?I'm very confused.
Last edited on
Ok,

so what does you code look like now?

Do you understand how I meant that:

ThisX = x[i];

and

1
2
int m = i+1; // avoid incrementing i
NextX = x[m];


Btw, I would prefer not to use i or j as variable names, they can cause problems because they are hard to see when there are lots of them in code.

Use a normal for loop:

for (Z = 0; Z < no_points;++Z)

:+)

Last edited on
Topic archived. No new replies allowed.