So I am writing a program which is moving circle through a line, I need the coordinates of line pixels, so I am using y = mx + b formula, but the coordinates of y don’t change if I use x++, can anyone tell me what i am doing wrong?
Here is a part of my code, where i am using this formula:
void draw_picture(Canvas & canvas) {
srand((unsigned)time(0));
PairXY a(200,400);
PairXY b(300,100);
int o=20;
Line l(a,b);
double x=0;
Circle cir(a,o);
draw_circle(cir, canvas);
draw_line(l, canvas);
x=a.x;
for (int i=20; i>0; i--){
x++;
///////
double m = (b.y-a.y)/(b.x-a.x);
double b1 = a.y - m * x;
double y = m * x + b1;
///////
a.x=x;
a.y=y;
Circle cir1(a,o);
draw_circle(cir1, canvas);
}
}
C++ does not use what you do with a value to influence how the value is computed. The fact that you are assigning these values to doubles does not cause them to be computed as doubles. Since the math is on integers, you get integer math, which is definitely not what you want.
One fix:
By forcing at least one parameter to be a double in each operation, you force the other to be promoted to a double as well and force the operation to be done on the doubles.
Note that the first line is only safe if
yis a signed type. If not,(b.y-a.y)could underflow. In that case, you need(b.y - (double) a.y).