Object as a mask


How can I do in order to get an object (obj1) as a mask on another one (obj2):
I want to see the intersection between obj1 and obj2 but not the parts of obj2 which are exterior.

I have search : intersect, clip in vain


Example : see joined pic, I want the triangle be filled in red, and i don’t want to see the yellow parts of the rectangle.
This is to fill the obj1 at a percentage of its height., the rectangle is its bound rectangle.

you just want the red, right? Do you want the black pen to trace all the way around the red (including the top), or is it ok if there is no black pen stroke where the clipping happens?


I want the red, yes.

It doesn’t matter to get the black pen stroke.

I have written a workaround for my needs, as the yellow rectangle represents a percentage of filling the form
see http://blog.ajornet.com/index.php?

Thanks for any another solution.


There’s probably a lot of ways to do it. Here’s one that clips the drawing as the Paint happens:

public class ClippedPolygon : GoPolygon {
public override void Paint(Graphics g, GoView view) {
RectangleF b = this.Bounds;
RectangleF rect = new RectangleF(b.Left, b.Top+40, b.Width, b.Height-40);
// handle clipping, if bounds is different from actual polygon
bool clipping = true; //this.Clipping;
Region oldClip = null;
Region newClip = null;
if (clipping) {
// intersect text rect with clipping rect
RectangleF cliprect = IntersectionRect(rect, g.ClipBounds);
oldClip = g.Clip;
newClip = new Region(cliprect);
g.Clip = newClip;
base.Paint(g, view);
if (clipping && oldClip != null)
g.Clip = oldClip;
if (newClip != null)
internal static RectangleF IntersectionRect(RectangleF a, RectangleF b) {
float maxx = Math.Max(a.X, b.X);
float maxy = Math.Max(a.Y, b.Y);
float minr = Math.Min(a.X + a.Width, b.X + b.Width);
float minb = Math.Min(a.Y + a.Height, b.Y + b.Height);
return new RectangleF(maxx, maxy, Math.Max(0, minr - maxx), Math.Max(0, minb - maxy));
compare with:
GoPolygon poly = new GoPolygon();
poly.AddPoint(100, 100);
poly.AddPoint(220, 0);
poly.AddPoint(280, 120);
poly.AddPoint(100, 100);
poly.BrushColor = Color.Blue;
ClippedPolygon poly2 = new ClippedPolygon();
poly2.AddPoint(100, 100);
poly2.AddPoint(220, 0);
poly2.AddPoint(280, 120);
poly2.AddPoint(100, 100);
poly2.BrushColor = Color.Red;
note... the "40", that's how much I clip the shape. You'd probably want to parameterize that.
Now... this has some issues. The selection area is still the full shape. But that might not matter if this is the foreground of some full sized object and this object isn't selectable.
You could override ComputeBounds to fix that.


it works fine.


A more general solution is to use a GoDrawing instead of a GoPolygon.