Lane Schwartz | 15 Mar 19:50
Picon
Gravatar

Trouble creating custom shape

Hi,

I'm new here. :) I'm having trouble drawing paths based on coordinates calculated from a user-specified
parameter. 

Here's what I have successfully done:
* Create a new shape using \pgfdeclareshape
* Inherit saved anchors, anchors, anchor border, and background path from rectangle
* Drawn lines to create extra rectangle in \foregroundpath. This will represent a bit vector.

Here's what I can't do:
* Draw cells within the bit-vector rectangle.

I want the user to specify, via parameters, the number of cells in the bit vector, and which cells should be
filled in.

For reference, I work in machine translation, and this shape is for use in a paper. The shape is essentially a
rectangle node (text inside the rectangle), plus a short bit-vector rectangle directly above the
primary node. The bit-vector rectangle should be the same width as the primary rectange, and the top edge
of the primary rectangle is also the bottom edge of the bit-vector rectangle. The bit vector rectangle
should be divided into a number of cells (based on a user-specified parameter), any of which might be
filled in (again, specified by the user).

Here's roughly what it should look like (use a fixed-width font to view the shape). In this example, the
first and third cells of the bit vector are filled in.

 -------------------
| X |   | X |   |   |
 -------------------
|                   |
|                   |
|                   |
|                   |
 -------------------

Here's the code I've used so far:

\pgfdeclareshape{hypothesis}{

	% Inherit anchors from rectangle
	\inheritsavedanchors[from=rectangle] 
	\inheritanchorborder[from=rectangle] 
	\inheritanchor[from=rectangle]{center} 
	\inheritanchor[from=rectangle]{north} 
	\inheritanchor[from=rectangle]{south} 
	\inheritanchor[from=rectangle]{west} 
	\inheritanchor[from=rectangle]{east}

	% Draw primary rectangle
	\inheritbackgroundpath[from=rectangle]

	% Draw the bit vector
	\foregroundpath{ 
		% store lower right in xa/ya and upper right in xb/yb
		\southwest \pgf <at> xa=\pgf <at> x \pgf <at> ya=\pgf <at> y 
		\northeast \pgf <at> xb=\pgf <at> x \pgf <at> yb=\pgf <at> y
		
		% Store top of bit vector rectangle in yc
		\pgf <at> yc=\pgf <at> yb \advance\pgf <at> yc by+10pt
				
		% Draw bit vector rectangle outline
		\pgfpathmoveto{\pgfpoint{\pgf <at> xb}{\pgf <at> yb}}
		\pgfpathlineto{\pgfpoint{\pgf <at> xb}{\pgf <at> yc}} 
		\pgfpathlineto{\pgfpoint{\pgf <at> xa}{\pgf <at> yc}} 
		\pgfpathlineto{\pgfpoint{\pgf <at> xa}{\pgf <at> yb}} 
		\pgfpathclose

		% Draw bit vector cells
		% ---------------------
		% 
		% This is the part where 
		%   I'm having a lot of trouble.
	}

}

Any guidance or tips would be greatly appreciated. So far I've read though much of the TikZ-PGF manual and
studied the source code for pgflibraryshapes.geometric.code.tex and
tikzlibrarycircuits.code.tex. Below is the current (broken) state of what I have to draw the bit vector cells:

		\def\totalwidth=\numexpr\pgf <at> xa-\pgf <at> xb
		\def\bitincrement=\numexpr1/\totalwidth
		\def\bitstartX=\pgf <at> xb
		\def\bitnextX=\numexpr\bitstartX+\bitincrement
		\def\bitfinalX=\numexpr\pgf <at> xa-\bitincrement

		\foreach \bitx in {\bitstartX,\bitnextX,...\bitfinalX } {
			\def\myq=10
			
			\pgfpathmoveto{\pgfpoint{\myq}{\pgf <at> yb}}
			\pgfpathclose
			%\let\myxval=\pgf <at> xb+\bitx
			%\pgf <at> xc=\pgf <at> xb \advance\pgf <at> xc by+\bitx
			%\pgfpathlineto{\pgfpoint{\myxval}{\pgf <at> yc}} 
		}

Thanks,
Lane

------------------------------------------------------------------------------
Download Intel&#174; Parallel Studio Eval
Try the new software tools for yourself. Speed compiling, find bugs
proactively, and fine-tune applications for parallel performance.
See why Intel Parallel Studio got high marks during beta.
http://p.sf.net/sfu/intel-sw-dev

Gmane