Squares within squares make for strange borders

If you're having trouble using Context Free or don't understand the language, ask for help here.

Moderators: MtnViewJohn, chris, mtnviewmark

Post Reply
Weisshaupt
Posts: 5
Joined: Mon Mar 02, 2009 2:11 pm

Squares within squares make for strange borders

Post by Weisshaupt »

This:

Code: Select all

rule B {
	SQUARE { s 20 1 }
	SQUARE { x -2.5 s 10 1 b .5 }
	SQUARE { x -2.5 s 7 1 b .7 }
	SQUARE { x -2.5 s 2.5 1 b 1 }
}
Produces this image:

Image

Making a square within a square and shrinking it only x-wise seems to shrink it a pixel or so y-wise.

This makes for unsightly banding and moire artifacts, like with this code:

Code: Select all

startshape A

rule A {
	50* { y 0.4 r -0.9 } B { }
}

rule B {
	SQUARE { s 20 1 }
	SQUARE { x -2.5 s 10 1 b .5 }
	SQUARE { x -2.5 s 7 1 b .7 }
	SQUARE { x -2.5 s 2.5 1 b 1 }
}
Which produces this:

Image

Anyone have any thoughts on how I work around this?

User avatar
MtnViewJohn
Site Admin
Posts: 882
Joined: Fri May 06, 2005 2:26 pm
Location: Mountain View, California
Contact:

Post by MtnViewJohn »

This is caused by anti-aliasing of the edges of the squares. In addition to all of the opaque black, gray, and white pixels that you are drawing there are translucent pixels at the edges of the squares. If you draw a translucent white pixel on top of a gray pixel you get gray, not white. This code will show you what happens when the edges of these four squares stack on top of each other:

Code: Select all

startshape foo

rule foo {
    SQUARE {a -0.5 b 0}
    SQUARE {a -0.5 b 0.5}
    SQUARE {a -0.5 b 0.7}
    SQUARE {a -0.5 b 1}
}
The solution is to use seven non-overlapping squares instead of four overlapping squares.

Weisshaupt
Posts: 5
Joined: Mon Mar 02, 2009 2:11 pm

Post by Weisshaupt »

MtnViewJohn wrote:This is caused by anti-aliasing of the edges of the squares. In addition to all of the opaque black, gray, and white pixels that you are drawing there are translucent pixels at the edges of the squares. If you draw a translucent white pixel on top of a gray pixel you get gray, not white. This code will show you what happens when the edges of these four squares stack on top of each other:

Code: Select all

startshape foo

rule foo {
    SQUARE {a -0.5 b 0}
    SQUARE {a -0.5 b 0.5}
    SQUARE {a -0.5 b 0.7}
    SQUARE {a -0.5 b 1}
}
The solution is to use seven non-overlapping squares instead of four overlapping squares.
Thanks, that worked much better.

User avatar
kipling
Posts: 91
Joined: Wed Jun 18, 2008 2:36 am

edge pixels

Post by kipling »

There are other strategies. For instance, you can force the drawing order so that all the blacks get laid down first, then greys, then whites. The "z" attribute is your friend here:

Code: Select all

startshape A

rule A {
   50* { y 0.4 r -0.9 } B { }
}

rule B {
   SQUARE { s 20 1 }
   SQUARE { x -2.5 s 10 1 b .5 z 1 }
   SQUARE { x -2.5 s 7 1 b .7 z 2 }
   SQUARE { x -2.5 s 2.5 1 b 1 z 3 }
} 

Weisshaupt
Posts: 5
Joined: Mon Mar 02, 2009 2:11 pm

Re: edge pixels

Post by Weisshaupt »

kipling wrote:There are other strategies. For instance, you can force the drawing order so that all the blacks get laid down first, then greys, then whites. The "z" attribute is your friend here:

Code: Select all

startshape A

rule A {
   50* { y 0.4 r -0.9 } B { }
}

rule B {
   SQUARE { s 20 1 }
   SQUARE { x -2.5 s 10 1 b .5 z 1 }
   SQUARE { x -2.5 s 7 1 b .7 z 2 }
   SQUARE { x -2.5 s 2.5 1 b 1 z 3 }
} 
Actually, you're still going to have trouble there. Try calling B {} just once and you'll see it looks exactly like my first picture.

The key here is to scale them y-wise just slightly.

Post Reply