Jump to content
  • Advertisement
Sign in to follow this  
i_love_pandas

geometry lines representation

This topic is 3626 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

i want to be able to tell if two lines intercept, say line a and b
line a(1,1,5,5);
line b(1,5,5,1);

if(intercept(a,b)){
    //code
}

but i've made if so it works exept for the "special" case of a vertical line. my representation of a line_equation is "y = mx + c", but a veritcal line m = infinity and for a horozontal line it is 0. so this made my code not work for these cases how should i model line equations to be able to tell if they intercept? "ax + by + c"?
#include <utility>
#include <iostream>
#include <limits>

template<typename T>
inline const T max(const T& a, const T& b){
	return (a>b?a:b);
}
template<typename T>
inline const T min(const T& a, const T& b){
	return (a<b?a:b);
}

namespace geometry{
	class point{
		public:
			typedef double num_type;
			point(num_type x=0, num_type y=0):
				_xx(x),_yy(y){
			}
			num_type& x(){return _xx;}
			num_type& y(){return _yy;}
			num_type x()const{return _xx;}
			num_type y()const{return _yy;}
		private:
			num_type _xx;
			num_type _yy;
	};
	class line{
		public:
			typedef double num_type;
			line(num_type x1=0, num_type y1=0, num_type x2=0, num_type y2=0):
				_1xx(x1),_1yy(y1),_2xx(x2),_2yy(y2){
			}
			line(const point& a, const point& b):
				_1xx(a.x()),_1yy(a.y()),_2xx(b.x()),_2yy(b.y()){
			}
			num_type& x1(){return _1xx;}
			num_type& y1(){return _1yy;}
			num_type& x2(){return _2xx;}
			num_type& y2(){return _2yy;}
			num_type x1()const{return _1xx;}
			num_type y1()const{return _1yy;}
			num_type x2()const{return _2xx;}
			num_type y2()const{return _2yy;}
		private:
			num_type _1xx;
			num_type _1yy;
			num_type _2xx;
			num_type _2yy;                
	};	    
	class old_line_equation{
		public:
			typedef double num_type;
			old_line_equation(const line& a):
			    _mm((a.y2()-a.y1())/(a.x2()-a.x1())){
				if(_mm==std::numeric_limits<double>::infinity())
					_cc = a.x1();
				else
				    _cc = (a.y1()-_mm*a.x1());
			}
			old_line_equation(num_type x1, num_type y1, num_type x2, num_type y2):
				_mm((y2-y1)/(x2-x1)),
				_cc(y1-_mm*x1){
			}
			num_type& m(){return _mm;}
			num_type& c(){return _cc;}
			num_type m()const{return _mm;}
			num_type c()const{return _cc;}
		private:
			num_type _mm;
			num_type _cc;
	};     
	const point interception(const old_line_equation& a, const old_line_equation& b){
		if(a.m()==std::numeric_limits<double>::infinity()){
			return point(a.c(),b.m()*a.c()+b.c());
		}
		if(b.m()==std::numeric_limits<double>::infinity()){
			return point(b.c(),a.m()*b.c()+a.c());
		}
		return point((b.c()-a.c())/(a.m()-b.m()),a.m()*(b.c()-a.c())/(a.m()-b.m())+a.c());
	}
	const bool lies_on(const point& p, const line& l){
		return (max(l.x1(),l.x2())>= p.x() && p.x() >= min(l.x1(),l.x2())&&
				max(l.y1(),l.y2())>= p.y() && p.y() >= min(l.y1(),l.y2()));
	}
	class line_equation{
		public:
			typedef double num_type;
			line_equation(const line& a){}
		private:
			num_type _aa;
			num_type _bb;
			num_type _cc;
	};
}

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!