c++ - a good-design approach for making a geometry library (regarding using union or not)? -


i making geometry library , confused, should return type of function calculates intersection of segment segment. returned value point , segment (overlap case) , empty set. per thinking there 3 ways tackle per follows: 1. return union (segment, null, point) 2. return segment first point == second point when intersection single point , both points nan when intersection empty set 3. return vector (with 0 elements empty set, 1 element pnt , 2 elements segment)

please let me know if there alternates possible , pros , cons of each of design. design supposed design , why. interested in making robust architecture lets single pipe-lining , no rewriting of code scalable (in terms of adding functionality , handling edge cases)

following code reference (whose return type vector)

vector<pnt> seg::inter(seg z){ vector<pnt> ans; if(p1==p2){if(z.doesinter(p1)){ans.pb(p1);}} else if(z.p1==z.p2){ if(doesinter(z.p1)) ans.pb(z.p1);} else{ pnt p1p2=(p2-p1); pnt q1=p1p2*pnt(0,1); long double h1,h2; if(abs((z.p2-z.p1).dot(q1))<=eps){ pnt r1((z.p1-p1)/(p2-p1)),r2((z.p2-p1)/(p2-p1)); if(abs(r1.y)<=eps){//colinear case h1=r1.x; h2=r2.x; if(h1>h2)swap(h1,h2); if(h2>=0&&h1<=1){//add eps h1=max(0.0l,h1);h2=min(1.0l,h2); ans.pb(p1+p1p2*h1); if(doublecompare(h1,h2)==-1)ans.pb(p1+p1p2*h2);}}} else{ h1 = ((p1-z.p1).dot(q1))/((z.p2-z.p1).dot(q1)); pnt q2 = (z.p2-z.p1)*pnt(0,1); h2 = ((z.p1-p1).dot(q2))/((p2-p1).dot(q2)); if(h1+eps>=0&&h1-eps<=1&&h2+eps>=0&&h2-eps<=1) ans.pb(z.p1+(z.p2-z.p1)*h1);}} return ans;} 

my suggestion create specialized intersection class can handle cases. can return instance of class then. internally class have example vector representation (with same endpoints if intersection 1 point, suggested) , have methods determining case (bool isintersecting(), issegment(), etc,).

for more sophisticated design, can make intersection class abstract , provide specialized implementations nointersection, pointintersection , segmentintersection different inner data representation.


Comments

Popular posts from this blog

android - Spacing between the stars of a rating bar? -

aspxgridview - Devexpress grid - header filter does not work if column is initially hidden -

c# - How to execute a particular part of code asynchronously in a class -