#include #include using namespace std; using namespace GiNaC; typedef lst::const_iterator lit; ex coeffmatrix(const ex&eq1,const ex&eq2,const ex&x,const ex&y) { return lst_to_matrix(lst( lst(eq1.coeff(x,1), eq1.coeff(y,1)), lst(eq2.coeff(x,1), eq2.coeff(y,1)) )); } ex columnvector(const ex&eq1,const ex&eq2,const ex&x,const ex&y) { return lst_to_matrix(lst( lst(-eq1.coeff(x,0).coeff(y,0)), lst(-eq2.coeff(x,0).coeff(y,0)) )); } int counttriangles(const lst&l, const ex&x, const ex&y) { int result=0; lit i,j,k; for(i=l.begin(); i!=l.end(); ++i) { for(j=i, ++j; j!=l.end(); ++j) for(k=j, ++k; k!=l.end(); ++k) { // check whether two lines are parellel. ex m1=coeffmatrix(*i,*j,x,y); if(ex_to(m1).determinant()==0) continue; ex m2=coeffmatrix(*i,*k,x,y); if(ex_to(m2).determinant()==0) continue; ex m3=coeffmatrix(*k,*j,x,y); if(ex_to(m3).determinant()==0) continue; ex c1=columnvector(*i,*j,x,y); ex c2=columnvector(*i,*k,x,y); // check wheter three lines intersect in one point. ex intersect1=evalm(ex_to(m1).inverse()*c1); ex intersect2=evalm(ex_to(m2).inverse()*c2); if(intersect1==intersect2) continue; cout << "lines " << *i << ", " << *j << ", " << *k << " form a triangle." << endl; ++result; } } return result; } int main() { symbol x("x"),y("y"); lst lines; lines = y, x-3*y, x-2*y, x-y, x+y-1, x+2*y-1, x+3*y-1; cout << "found " << counttriangles(lines,x,y) << " triangles."<< endl; return 0; }