HI all, Still having problems getting an output from my line program.
As an overview:
I have an array of objects called LineArray. Each of these objects has an arrary called m_XYCoOrds[][]. This is a ten by two array. The ten is the number of points that make up a line, the two is the x y co-ords where [0] is x and [1] is y. e.g. the second point x co-ordinate is given by
m_XYCoOrds[1][0]
and its y by
m_XYCoOrds[1][1]
I have a function called ncrosses that will get the number of illegal zones that a line passes through. This function is called ncrosses.
Code:
int ncrosses(int m_XYCoOrds[][2])
{
poly4toline4();
int NumLines = 0;
int x1 = 0;
int y1 = 0;
int x2 = 0;
int y2 = 0;
for(int t=0;t<9;t++)
{
x1 = m_XYCoOrds[t][0];//get first point x
y1 = m_XYCoOrds[t][1];//get first point y
x2 = m_XYCoOrds[t+1][0];//get second point x
y2 = m_XYCoOrds[t+1][1];//get second point y
struct lineSeg *l1;
int i,j,count=0;
l1=coords2lineSeg(x1,y1,x2,y2);
for(i=0;i<MSIZE;i++)//msize is the number of illegal zone
{ for(j=0;j<NSIDES;j++)//nsides are the number of sides to an illegal zone
if(crosses(l1,lmap[i].ln[j]))
{
count++;
/*printf("hit %d ",i); */ /* only need to know about one line
crossed to know zone crossed or partially
crossed, could modify to get more info.*/
break;
}
}
free(l1);
NumLines = NumLines + count;//add the total number of zones crossed by this section to the whole line coune
}
return NumLines;//return the total line count
}
It takes a start point and the next one and passes these on to the function crosses. Which returns a 0 if no lines are crossed or a 1 if they do.
Code:
int crosses(struct lineSeg *l1, struct lineSeg *l2)
{
poly4toline4();
int A,B,k,x1diff,y1diff,x2diff,y2diff;
x1diff = (l1->r2.i - l1->r1.i);
y1diff = (l1->r2.j - l1->r1.j);
x2diff = (l2->r2.i - l2->r1.i); /* x2diff and y2diff cannot be zero at
same time*/
y2diff = (l2->r2.j - l2->r1.j);
/* boundary cases ****************************************************/
if((x1diff ==0 && x2diff==0 && l1->r1.i != l2->r1.i )
|| (y1diff==0 && y2diff==0 && l1->r1.j != l2->r1.j))
return 0; /*parallel */
if(x1diff ==0 && x2diff==0 && l1->r1.i == l2->r1.i)
{if((between(l1->r1.j,l2->r1.j,l2->r2.j) ||
between(l1->r2.j,l2->r1.j,l2->r2.j)))
return 1; /*parallel but touching */
else return 0;
}
if(y1diff==0 && y2diff==0 && l1->r1.j == l2->r1.j)
{if((between(l1->r1.i,l2->r1.i,l2->r2.i) ||
between(l1->r2.i,l2->r1.i,l2->r2.i)))
{return 1;} /*parallel but touching*/
else return 0;
}
if(x1diff==0 && y1diff==0) /* actually a point */
{A=(l1->r1.i - l2->r1.i)/x2diff;
if(0<=A && A<=1 && A== ((l1->r1.j - l2->r1.j)/y2diff))
{return 1;}
else return 0;
}
if(x1diff==0)
{ B=(l1->r1.i - l2->r1.i)/x2diff;
if(0<=B && B<=1)
{A=(l2->r1.j + B*(y2diff) - l1->r1.j)/y1diff;
if(0<=A && A<=1)
{
return 1;
}
else return 0;
}
else return 0;
}
if(x2diff==0)
{A=(l2->r1.i - l1->r1.i)/x1diff;
if(0<=A && A<=1)
{B=(l1->r1.j + (A*y1diff) - l2->r1.j)/y2diff;
if(0.0 <= B && B <= 1.0)
{
return 1;
}
else return 0;
}
else return 0;
}
if(y1diff==0)
{B=(l1->r1.j - l2->r1.j)/y2diff;
if(0<=B && B<=1)
{A=(l2->r1.i + B*x2diff - l1->r1.i)/x1diff;
if(0<=A && A<=1)
{
return 1;
}
else return 0;
}
else return 0;
}
if(y2diff==0)
{A=(l2->r1.j - l1->r1.j)/y1diff;
if(0<=A && A<=1)
{B=(l1->r1.i + A*x1diff - l2->r1.i)/x2diff;
if(0<=B && B<=1)
{
return 1;
}
else return 0;
}
else return 0;
}
/* standard cases ********************************************/
/* else see if values between 0 and 1 for A and B for vector eqn.
r1 + A(r2-r1) = R1 + B(R2-R1), wher r1,r2 and R1,R2 are vectors of end pnts of
line segments */
if(y1diff != 0 && y2diff != 0)
{k=((x2diff*y1diff/x1diff) - y2diff);
if(k==0) /* parallel, singularity */
{A=(l1->r1.i - l2->r1.i)/x2diff;
if(0<=A && A<=1 && A== ((l1->r1.j - l2->r1.j)/y2diff))
{ return 1;} /*2 lines coincident*/
else
{A=(l1->r2.i - l2->r1.i)/x2diff;
if(0<=A && A<=1 && A== ((l1->r2.j - l2->r1.j)/y2diff))
{ return 1;} /*2 lines coincident*/
else return 0; /* parallel */
}
}
else
{B=((l2->r1.j) + ((l1->r1.i)*y1diff/x1diff) - (l1->r1.j + ((l2->r1.i)*y1diff/x1diff)))/k;
if(0<=B && B<=1)
{A = ((l2->r1.i/x1diff) + B*(x2diff/x1diff)-((l1->r1.i)/x1diff));
if(0<=A && A<=1)
{ return 1;}
else return 0;
}
else return 0;
}
}
return 0;
}
the function poly4toline4() converts map
into a format more suitable for cross checking via vector equations.
when i ran this program from a command line everything went very well. The problem here is that the ncrosses function ALWAYS returns a zero. I don't understand what i've done wrong when implementing this program into MFC.
below is the calling section for the ncrosses funciton
Code:
for(int i=0; i<10;i++)
{
LineArray[i].RandomCoOrd();//generate all the random coords
LineArray[i].m_Length = LineArray[i].SectionLength(LineArray[i].m_XYCoOrds);//get length of line
temp = ncrosses(LineArray[i].m_XYCoOrds);//get the number of lines crossed
if(temp ==0)
temp =50;
total = total + temp;
LineArray[i].m_LineCrosses = total;
//LineArray[i].m_Penalty = LineArray[i].m_LineCrosses*10;//get the penalty value
//LineArray[i].m_Cost = LineArray[i].m_Length + LineArray[i].m_Penalty;
OnOffArray[i] = 0;//set all lines to off
}
I did not write the crosses function but know that it works. I can't see any reason why this won't work now its in MFC. I'm tearing my hair out over this one so any help whatsoever would be greatly appreciated.
Many thanks
Dylan