🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.postscript
Article #1950

Re: graduated triangle fills?

#1950
From: Ross Cunniff
Date: Mon, 14 Sep 1992 17:20
78 lines
3100 bytes
nigel@maths.su.oz.au (Nigel O'Brian) writes:
>This might be a bit optimistic, but does anyone have a routine
>to do a graduated grayscale fill inside a triangular path? 
>Something like
>
>x1 y1 x2 y2 x3 y3 gray1 gray2 gray3 gradfill
>
>producing a triangle with specified vertices, and a more or less
>linearly varying gray taking the specified values at the vertices.
>I want it for 3D shading. Something recursive should
>work -- maybe generating 4 invocations corresponding to
>the subdivision of the triangle given by joining mid-points of 
>sides. Depth can be controlled by maximum variation of grays
>at the vertices. Perhaps such a routine would be of general
>interest? 

Ask and you shall receive :-)

---cut here------------cut here---------------cut here----
% Trigrad.ps - fill a triangle with a gradient
% Arguments: x1 y1 gray1 x2 y2 gray2 x3 y3 gray3 trigrad
% Calls itself recursively.

% Ross Cunniff, Hewlett-Packard Graphics Software Lab, cunniff@fc.hp.com
% September 14, 1992

% This parameter defines the maximum gray delta allowed to draw a single
% triangle
/_maxgraydel 1 15 div def

% This is the dictionary used by the procedure
/_tridict 10 dict def

/trigrad { _tridict begin
    /gray3 exch def /y3 exch def /x3 exch def
    /gray2 exch def /y2 exch def /x2 exch def
    /gray1 exch def /y1 exch def /x1 exch def
    % Test the size of the triangle
    /maxg gray1 gray3 sub dup 0 le { neg } if def
    gray1 gray2 sub dup 0 le {neg} if dup maxg gt {/maxg exch def} {pop} ifelse
    gray2 gray3 sub dup 0 le {neg} if dup maxg gt {/maxg exch def} {pop} ifelse
    maxg _maxgraydel le {
	% Triangle is 'small' - just fill it and return
	x1 y1 moveto x2 y2 lineto x3 y3 lineto closepath
	gray1 gray2 add gray3 add 3 div setgray
	fill
	end
    } { %else
	newpath
	% Subdivide into four triangles and recurse
	x1 y1 gray1
	x1 x2 add 2 div y1 y2 add 2 div gray1 gray2 add 2 div
	x1 x3 add 2 div y1 y3 add 2 div gray1 gray3 add 2 div
	x1 x2 add 2 div y1 y2 add 2 div gray1 gray2 add 2 div
	x2 y2 gray2
	x2 x3 add 2 div y2 y3 add 2 div gray2 gray3 add 2 div
	x1 x3 add 2 div y1 y3 add 2 div gray1 gray3 add 2 div
	x3 x2 add 2 div y3 y2 add 2 div gray3 gray2 add 2 div
	x3 y3 gray3
	x1 x2 add 2 div y1 y2 add 2 div gray1 gray2 add 2 div
	x2 x3 add 2 div y2 y3 add 2 div gray2 gray3 add 2 div
	x1 x3 add 2 div y1 y3 add 2 div gray1 gray3 add 2 div
	end
	% Trade off stack space for VM space...
	trigrad
	trigrad
	trigrad
	trigrad
    } ifelse
} bind def

---cut here------------cut here---------------cut here----

>Nigel O'Brian

Ross Cunniff
Hewlett-Packard Graphics Software Lab
cunniff@fc.hp.com
Technical Details
Message-ID: <7090003@hpfcso.FC.HP.COM>
Path: arc1-ca.pugleaf.net!sparky!uunet!cs.utexas.edu!swrinde!sdd.hp.com!scd.hp.com!hplextra!hpfcso!cunniff
References: <NIGEL.92Sep8151038@frog.maths.su.oz.au>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!sparky!uunet!cs.utexas.edu!swrinde!sdd.hp.com!scd.hp.com!hplextra!hpfcso!cunniff
From: cunniff@hpfcso.FC.HP.COM (Ross Cunniff)
Newsgroups: comp.lang.postscript
Subject: Re: graduated triangle fills?
Message-ID: <7090003@hpfcso.FC.HP.COM>
Date: 14 Sep 92 17:20:33 GMT
References: <NIGEL.92Sep8151038@frog.maths.su.oz.au>
Organization: Hewlett-Packard, Fort Collins, CO, USA
Lines: 78