c# - Drawing an envelope around a curve -


in c# winforms application have picturebox hosts 2 curves (resulted voltage/current measurement). x axis voltage , y axis current. voltage axis ranged -5 5 current axis smaller scale ranged -10 ua 10 ua. task see if second curve within 10% of first curve.

for visual inspection trying draw envelope around first curve (blue one). curve pointf array. @ moment since have no idea how draw correct envelope around blue curve, draw 2 other curves result of x points of actual curve added , subtracted 10% of original curve. of course bad approach, atleast section of curve noticably vertical, works. curve on non vertical section, trick not work anymore, can see in picture below:

enter image description here

here code using draw envelope:

public bitmap drawenvelope(double[,] pindata, float vlimit, float ilimit) {     g = graphics.fromimage(box);     g.smoothingmode = smoothingmode.antialias;     g.pixeloffsetmode = pixeloffsetmode.highquality;      pointf[] u = new pointf[pindata.getlength(0)]; //up line     pointf[] d = new pointf[pindata.getlength(0)]; //down line     list<pointf> joinedcurves = new list<pointf>();      float posx = xmaxvalue * (vlimit / 100);     float minx = posx * -1;       (int = 0; < pindata.getlength(0); i++)     {         u[i] = new pointf(400 * (1 + (((float)pindata[i, 0]) + minx) / (xmaxvalue + vexpand)), 400 * (1 - ((float)pindata[i, 1] * getinvers((ymaxvalue + iexpand)))));     }      (int = 0; < pindata.getlength(0); i++)     {         d[i] = new pointf(400 * (1 + (((float)pindata[i, 0]) + posx) / (xmaxvalue + vexpand)), 400 * (1 - ((float)pindata[i, 1] * getinvers((ymaxvalue + iexpand)))));     }       pen pengraph = new pen(color.fromargb(50, 0 ,0 ,200), 1f);     pengraph.alignment = penalignment.center;      joinedcurves.addrange(u);     joinedcurves.addrange(d.reverse());      pointf[] fillpoints = joinedcurves.toarray();     solidbrush fillbrush = new solidbrush(color.fromargb(40, 0, 0, 250));     fillmode newfillmode = fillmode.alternate;      g.fillclosedcurve(fillbrush, fillpoints, newfillmode, 0);      g.dispose();     return box; } 

the green circles added myself, , indicate region second curve (red one) potentially has difference bigger 10% orginal curve.

would nice if put me in right way, should to achive nice envelope around original curve?

update because noob cant find way implement answers given question until now, put bounty see if somone can kindly show me atleast coding approach problem.

your best bet iterate point array , calculate perpendicular vector 2 consecutive points each time (see calculating 2d vector's cross product implementation clues). project in either direction along these perpendicular vectors generate 2 point arrays of envelope.

this function generates them using segment midpoints (as long point count high , offset not small should ok when plotted):

private void getenvelope(pointf[] curve, out pointf[] left, out pointf[] right, float offset)         {             left = new pointf[curve.length - 1];             right = new pointf[curve.length - 1];              (int = 1; < curve.length; i++)             {                 pointf normal = new pointf(curve[i].y - curve[i - 1].y, curve[i - 1].x - curve[i].x);                 float length = (float)math.sqrt(normal.x * normal.x + normal.y * normal.y);                 normal.x /= length;                 normal.y /= length;                  pointf midpoint = new pointf((curve[i - 1].x + curve[i].x) / 2f, (curve[i - 1].y + curve[i].y) / 2f);                 left[i - 1] = new pointf(midpoint.x - (normal.x * offset), midpoint.y - (normal.y * offset));                 right[i - 1] = new pointf(midpoint.x + (normal.x * offset), midpoint.y + (normal.y * offset));             }         } 

Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -