What does "int ix = ( delta_x > 0 ) - ( delta_x < 0 )" do

Well, what does it do ?

also, what is the modern pascal's equivalent ? I am trying to convert the Bresenham's Line Algorithm from C++ http://www.roguebasin.com/index.php?title=Bresenham%27s_Line_Algorithm to pascal
Last edited on
closed account (E0p9LyTq)
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>

int ix(int);

int main()
{
   std::cout << ix(-5) << ' ' << ix(0) << ' ' << ix(20) << '\n';
}

int ix(int delta_x)
{
   return ((delta_x > 0) - (delta_x < 0));
}

-1 0 1
(delta_x > 0) and (delta_x < 0) return bool values. When you apply arithmetic operations to bools true will be treated as 1 and false as 0.

Using the conditional (ternary) operator it could have been written as:
 
int ix = (delta_x > 0 ? 1 : 0) - (delta_x < 0 ? 1 : 0)

Using if statements it could be rewritten as:
1
2
3
4
5
6
7
8
9
10
11
12
13
int ix;
if (delta_x > 0)
{
	ix = 1;
}
else if (delta_x < 0)
{
	ix = -1;
}
else //delta_x == 0
{
	ix = 0;
}

I have no idea how to write this in Pascal.
Last edited on
well, that's interesting, thanks for the help, the second program you gave me @Peter87 can be translated to pascal pretty easily

1
2
3
if ( delta_x > 0 ) then ix := 1
else if ( delta_y ) then ix := -1
else ix := 0;
Topic archived. No new replies allowed.