SYNOPSIS

DESCRIPTION
The
VG interface allows applications to construct, display, export and import vector
drawings, which are composed of entities (i.e.,
VG_Node objects) from simple primitives to more complex or applicationspecific items.
There is no notion of coordinates in VG. Entities are organized in a tree structure where elements are connected by linear transformations (such as translations or rotations). References between the entities are allowed. For example, a VG_Line(3) is fully described by references to two independent VG_Point(3) entities. The VG_View(3) widget is almost always used to display VG drawings. VG_View also provides a handy "tool" registration interface which simplifies the development of editors. Note: While VG is generalpurpose and designed for simplicity, a more CAD oriented alternative, SK, is available in the FreeSG distribution Pq Lk http://freesg.org/ .SK is similar to VG, but it provides more advanced math and computational geometry features. Unlike VG which uses linear transformations to represent relative entity positions, SK uses systems of geometrical constraints (e.g., distances, angles, etc.). 
BUILTIN NODE CLASSES
A number of simple primitives entities are built into the library:

VG INTERFACE
NODE CLASS REGISTRATION
Applications and utilities are expected to register node classes using VG_RegisterClass(), which registers the class described by the given VG_NodeOps structure. VG_UnregisterClass() unregisters the given class. VG_LookupClass() searches for a class of the specified name and return its description, or NULL if there is no such class. The VG_NodeOps structure fully describes a VG node class. All function pointers are optional and can be set to NULL.
The name field is a string identifier for this class. icon is an optional Agar icon resource for GUI purposes. size is the full size in bytes of the structure (derived from VG_Node) which describes node instances. The init() operation initializes a node instance structure. destroy() releases resources allocated by the node instance. load() and save() are used to (de)serialize the node instance from/to the given AG_DataSource(3). The draw() operation graphically renders the entity in a VG_View(3) context, typically using the standard AG_Widget(3) draw routines. extent() computes the axisaligned bounding box of the entity, returning the absolute VG coordinates of the upperleft corner in a and the lower right corner in b. pointProximity() computes the shortest distance between p (given in absolute VG coordinates) and the entity. This operation is needed for GUI selection tools to be effective. lineProximity() computes the shortest distance between the line (as described by endpoints p1 and p2) and the entity. This is an optimization which is optional to the operation of GUI selection tools. The deleteNode() callback is invoked when the user deletes the node instance. It is used, for example, by VG_Line(3) to call VG_DelRef() on its two VG_Point(3) references (also calling VG_Delete() if their reference count reaches zero). The moveNode() callback is invoked by VG_View(3) tools (usually "select" tools) to perform a translation on the entity. vAbs is the desired new position in absolute VG coordinates, vRel describes the change in position. It is recommended to only rely on vRel. The edit() callback is invoked by the VG_EditNode() operation of VG_View(3). It is expected to return a container widget to which is attached a number of widgets bound to various VG_Node instance parameters. 
NODE OPERATIONS
The VG_NodeInit() function completely initializes a VG_Node structure as an instance of the specified node class. VG_NodeIsClass() returns 1 if the specified node is an instance of the given class, 0 otherwise. VG_NodeAttach() and VG_NodeDetach() are used to construct the hierarchy of entities in a drawing. The relationship between parent and child nodes defines the order of linear transformations (i.e., translations, rotations). VG_NodeAttach() attaches node to an existing node parent (which is either the root member of the VG structure, or any other entity in the drawing). VG_NodeDetach() detaches the specified node from its current parent. The VG_Delete() function detaches and frees the specified node instance, along with any child nodes. The function can fail (returning 1) if the entity is in use. VG_AddRef() creates a new reference (dependency), where node depends on refNode. VG_DelRef() removes the dependency with refNode and returns the new reference count of refNode. This allows the referenced node to be automatically deleted when no longer referenced. Under multithreading, the return value of VG_DelRef() is only valid as long as VG_Lock() is used. VG_SetSym() sets the symbolic name of the node, an arbitrary userspecified string which allows the node to be looked up using VG_FindNodeSym(). VG_SetLayer() assigns the node to the specified layer number (see VG_PushLayer() and VG_PopLayer()). VG_SetColorv() sets the node color from a pointer to a VG_Color structure. VG_SetColorRGB() sets the node color from the given RGB triplet. VG_SetColorRGBA() sets the node color from the given RGBA components. The VG_Select() and VG_Unselect() functions respectively set and unset the selection flag on the node. VG_SelectAll() and VG_UnselectAll() operate on all nodes in the drawing. Nodes are named by their class name followed by a numerical handle (e.g., the first line created in a drawing will be named Line0). VG_GenNodeName() generates a new name, unique in the drawing, for use by a new instance of the specified class. The VG_FindNode() function searches for a node by name, returning a pointer to the specified instance or NULL if not found. The VG_FindNodeSym() variant searches node by their symbolic names (see VG_SetSym()). Under multithreading, the return value of both VG_FindNode() and VG_FindNodeSym() only remains valid as long as VG_Lock() is used. 
LINEAR TRANSFORMATIONS
Every node in a VG is associated with an invertible 3x3 matrix T, which defines a set of transformations on the coordinates. The VG_Pos() function computes the current absolute VG coordinates of the node. VG_LoadIdentity() sets the transformation matrix of the node to the identity matrix. VG_Translate() translates the node by the amount specified in v. VG_SetPosition() assigns the node an absolute position v, relative to the VG origin. VG_SetPositionInParent() assigns a position relative to the parent node. VG_Scale() uniformly scales the node by a factor of s. VG_Rotate() rotates the node by the specified amount, given in radians. VG_FlipVert() mirrors the node vertically and VG_FlipHoriz() mirrors the node horizontally. VG_NodeTransform() computes and returns into T the product of the transformation matrices of the given node and those of its parents. VG_PushMatrix() and VG_PopMatrix() are called from the draw() operation of nodes to manipulate the global matrix stack associated with a drawing during rendering. VG_PushMatrix() grows the stack, duplicating the top matrix. VG_PopMatrix() discards the top matrix. VG_MatrixInvert() computes the inverse of M. Since VG matrices are required to be nonsingular, this operation cannot fail. 
SERIALIZATION
The VG_Save() function archives the contents of vg into the specified data source. VG_Load() loads the drawing from a data source; see AG_DataSource(3). VG_ReadVector() and VG_WriteVector() are used to (de)serialize vectors (see MATH ROUTINES section). VG_ReadColor() and VG_WriteColor() are used to (de)serialize VG_Color structures. VG_WriteRef() is useful for serializing a reference from one node to another. For example, the VG_Line(3) save() routine simply consists of VG_WriteRef() calls on its two VG_Point(3) references ) . VG_ReadRef() deserializes a node>node reference. If className is provided, the function will fail and return NULL if the archived reference does not match the specified class name. 
COLOR OPERATIONS
VG_GetColorRGB() returns the VG_Color structure describing the specified RGB triplet, with the alpha component set to 1.0 (opaque). The VG_GetColorRGBA() variant includes the alpha component. Functions VG_MapColorRGB() and VG_MapColorRGBA() convert the given color to AG_Color(3) format. VG_BlendColors() blends the two specified colors, returning the results in cDst. VG_SetBackgroundColor() configures the background color of the drawing. The VG_SetSelectionColor() and VG_SetMouseOverColor() functions configure the color which will be blended into the graphical rendering of entities which are selected or under the cursor, respectively. 
MATH ROUTINES
The VG_GetVector() function returns a VG_Vector structure given x, y values. The VG_MatrixIdentity() function returns the identity matrix. VG_Add() returns the sum of vectors v1 and v2. VG_Sub() returns the difference of vectors v1 and v2. VG_ScaleVector() multiplies vector v by a scalar c. VG_DotProd() returns the dot product of two vectors. VG_Length() returns the length of a vector. VG_Distance() returns the unsigned distance between two vectors. VG_PointLineDistance() computes the minimal distance from a line (described by two points A and B) and a point pt. The function returns the distance, and the closest point on the line is returned back into pt. VG_IntersectLineV() computes the intersection of an infinite line (described by p1 and p2) against a vertical line (described by x). The return value is undefined if the two lines are parallel. VG_IntersectLineH() performs the same operation against a horizontal line (described by y). VG_MultMatrix() computes the product of matrices A and B, returning the result into B. VG_MultMatrixByVector() computes the product of matrix M and vector v, returning the result in Mv. 
SEE ALSO
AG_Intro(3), VG_Arc(3), VG_Circle(3), VG_Line(3), VG_Point(3), VG_Polygon(3), VG_Text(3), VG_View(3) 
HISTORY
The VG interface first appeared in Agar 1.3.3. 