[cairo] Switching dashes on and then off

Alan Battersby alan.battersby at ntlworld.com
Thu Apr 22 13:37:27 PDT 2010


I am using mono and Cairo in c# to draw some paths (my terminology not
Cairo paths) which are a list of vertices. I want visible vertices to be
connected by solid lines and hidden vertices to be connected by dashed
lines. Thus the path will switch between solid and dashed as the path
becomes hidden behind something and then reappears. The states ENTER and
XIT are where the path meets and leaves the area hiding it. I use these
values to colour the nodes if that option is chosen.

I cannot seem to get the type of behavior I want using the code below.
Can someone please tell me what I am doing wrong.

Thanks

Alan

double[] empty = new double[]{};
double[] dash  = new double[]{2,2};

void switchDashOn(bool on,Cairo.Context ct)
{
	ct.StrokePreserve();
	if (on)
		ct.SetDash(dash,0);
	else
		ct.SetDash(empty,0);
}

public void Draw2D(Cairo.Context ct,
		   Transformation t,
		   bool close)
{
	Vertex v;
	bool dashesOn = false;
			
	if (this.Count == 0) return;
	Cartesian3 cp = t.Transform(this[0]).Coordinate;
	ct.MoveTo(cp.X,cp.Y);
	for (int i=1; i < this.Count; i++)
	{
		v = this[i];
		cp = t.Transform(v).Coordinate;
			
		if (v.State == VSTATE.NORMAL) { // should be solid lines
			
			if (dashesOn) {
				switchDashOn(false,ct);
				dashesOn = false;
			}
			ct.LineTo(cp.X,cp.Y);
		}
		else if (v.State == VSTATE.ENTER) { // switch dashed on
			if (!dashesOn) {
				switchDashOn(true,ct);
				dashesOn = true;
			}
			ct.LineTo(cp.X,cp.Y);
		}
		else if (v.State == VSTATE.XIT) { // switch it off
			if (dashesOn) {
				switchDashOn(false,ct);
				dashesOn = false;
			}
			ct.LineTo(cp.X,cp.Y);
		}
		else if (v.State == VSTATE.HIDDEN) { // should be dashed
			if (!dashesOn) {
				switchDashOn(true,ct);
				dashesOn = true;
			}
			ct.LineTo(cp.X,cp.Y);
		}
	}
	if (close) {
		// draw last point to first	
		cp = t.Transform(this[0]).Coordinate;
		ct.LineTo(cp.X,cp.Y);
	}
}





More information about the cairo mailing list