views:

39

answers:

1

I have a scribbale inkpresenter in my silverlight app that I'm using multiple times in different shapes, I want to calculate how much percent of the inkpresenter has been scribbled,

My problem is that since the shape of the ink presenter can be circular or poly I don't know how i can get the units/pixels there is available on the scribbable surface.

As far as i understand is that the actualWidth and actualHeight properties will only help me for rectangular shape by calculating

units = uielemnt.Actualwidth * uielement.ActualHeight

but for costum shapes I'm not sure how the actualWidth/Height are calculated.

A: 

WPF Answer

In WPF you can just use Geometry.Combine to combine the stroke geometries, then call GetArea on the combined geometry to get the total area covered.

Silverlight Answer

In Silverlight you don't have Geomerty.Combine or Geometry.GetArea, and you also don't have RenderTargetBitmap. So in Silverlight I think you have to do the calculations yourself.

Here are the setup steps:

  1. Convert the geometries to cubic bezier curves (approximate arcs, all else are exact)
  2. Split all curves at their x minima and x maxima
  3. Rearrange coordinates so each curve goes from low x to high x
  4. Sort by x then y of first coordinate
  5. Start at minimum x coordinate with no active curve segments

Here is the main loop:

  1. Add any bezier curves that start at the current x coordinate to the active list
  2. Remove any bezier curves that end at the current x coordinate from the active list
  3. Compute intersections of active bezier curves. Find next x coordinate of such intersection.
  4. Let "end x" be earliest of next line intersection and next start or stop coordinate of a bezier curve
  5. Compute area from current x to "end x" then set current to end
  6. Repeat until "end x" reaches macimum x coordinate of any curve

Here is the inner loop that computes the area:

  1. Go through active curves in increasing y.
  2. Every time a curve is passed flip the "inside" bit for the original geometry for that curve.
  3. Every time y changes any "inside" bit is set, compute the area between the previous bezier curve and the current one and add it to the grand total area

That's it.

More details on Silverlight answer

Formulas to convert segments of a PathGeometry into a cubic bezier approximation:

  • BezierSegment: Cubic control points are P0, P1, P2, P3 (no change)
  • LineSegment: Cubic control points are P0, P0, P1, P1
  • QuadraticBezierSegment: Cubic control points are P0, (P0+2P1)/3, (2P1+P2)/3, P2
  • ArcSegment: Divide into octants and for each, compute unit tangent vectors T0, T1 at start & end pointing toward arc, generate control points (approximation) of P0, P0 + T0 * 4/3*(Sqrt(2)-1)*R, P1 + T1 * 4/3(Sqrt(2)-1)*R, P1, then modify with size and RotationAngle
  • Poly...Segment: Break into individual segments, then use above formulas on each

Computing intersections of active Bezier curves:

  • Since the curves have already been split at their minima / maxima, each is a smooth, continuous function of x, you can use these techniques to compute their intersection
  • It can save some computation if you check y minima/maxima to see if there is any possible overlap before actually computing the intersection
Ray Burns
Perhaps you could elaborate on "Convert the geometries to cubic bezier curves" and "Compute intersections of active bezier curves"?
Gabe
Done. ---------
Ray Burns