Sign in to follow this  

geometry lines representation

Recommended Posts

i_love_pandas    100
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);


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{
			typedef double num_type;
			point(num_type x=0, num_type y=0):
			num_type& x(){return _xx;}
			num_type& y(){return _yy;}
			num_type x()const{return _xx;}
			num_type y()const{return _yy;}
			num_type _xx;
			num_type _yy;
	class line{
			typedef double num_type;
			line(num_type x1=0, num_type y1=0, num_type x2=0, num_type y2=0):
			line(const point& a, const point& b):
			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;}
			num_type _1xx;
			num_type _1yy;
			num_type _2xx;
			num_type _2yy;                
	class old_line_equation{
			typedef double num_type;
			old_line_equation(const line& a):
					_cc = a.x1();
				    _cc = (a.y1()-_mm*a.x1());
			old_line_equation(num_type x1, num_type y1, num_type x2, num_type y2):
			num_type& m(){return _mm;}
			num_type& c(){return _cc;}
			num_type m()const{return _mm;}
			num_type c()const{return _cc;}
			num_type _mm;
			num_type _cc;
	const point interception(const old_line_equation& a, const old_line_equation& b){
			return point(a.c(),b.m()*a.c()+b.c());
			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{
			typedef double num_type;
			line_equation(const line& a){}
			num_type _aa;
			num_type _bb;
			num_type _cc;

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this