Tscope5
graphics.c File Reference

Go to the source code of this file.

Functions

Coordinate functions

There are two coordinate systems and two coordinate scales available in Tscope5.

  • There is a Display coordinate system, with the origin (0,0) in the upper left corner of the display, with increasing coordinate values to the right of and below the origin.
  • There is also a Cartesian system, with the origin (0,0) in the center of the display and increasing coordinate values to the right and above the origin, and decreasing (negative) values to the left and below the origin.
  • Default is the Cartesian system.
  • There is an absolute coordinate scale where coordinates are interpreted as pixel values.
  • There is a relative coordinate scale where coordinates are interpreted relative to the display size.
  • The default is the absolute system.

Some helper functions that (can help you) convert between the coordinate scales/systems, but most of the time you will only need to choose the coordinate system/scale and Tscop5 will automatically do the transormations that are necessary.

int ts5_set_coordinate_system (const int coordinate_system)
int ts5_get_coordinate_system ()
int ts5_set_coordinate_scale (const int coordinate_scale)
int ts5_get_coordinate_scale ()
void ts5_get_target_size (double *w, double *h)
double ts5_get_target_width ()
double ts5_get_target_height ()
double ts5_relative_to_absolute_coordinate_x (const double x)
double ts5_relative_to_absolute_coordinate_y (const double y)
double ts5_absolute_to_relative_coordinate_x (const double x)
double ts5_absolute_to_relative_coordinate_y (const double y)
double ts5_cartesian_to_display_coordinate_x (const double x)
double ts5_cartesian_to_display_coordinate_y (const double y)
double ts5_display_to_cartesian_coordinate_x (const double x)
double ts5_display_to_cartesian_coordinate_y (const double y)
Color definitions

Tscope5 describes colors in a structure (TS5_COLOR) containing red (r), green (g), blue (b) and alpha (a, transparency) values between 0.0 and 1.0.

The functions below generate such a color structure using the RGB, HSL or HSV color specification or using the w3c naming system.

Please note that the color structure is a structure, not a variable (they can only be assigned at the moment of declaration, not afterwards). See the examples for instructions on how to use them.

TS5_COLOR ts5_make_rgb_color (const double r, const double g, const double b, const double a)
TS5_COLOR ts5_make_hsl_color (const double h, const double s, const double l, const double a)
TS5_COLOR ts5_make_hsv_color (const double h, const double s, const double v, const double a)
TS5_COLOR ts5_make_named_color (const char *name, const double a)
Graphics parameter functions

This part contains functions to change the graphics parameters of the text output and primitives drawing functions.

  • By default, text appears 20pt Courier Bold.
  • Primitives are not filled, and drawn with hairline thickness.
  • All drawing is done in white against a black background.

The text and drawing functions do not have arguments to alter the graphics parameters. This is done with the parameter functions below. They all change one parameter, e.g. the foreground color. Once a parameter value is changed, that value is used for all further drawing until it is changed again.

The parameter functions all return the previous parameter setting. This can be useful when you want to change some parameters in one part of a program, do some drawing and set the previous parameters again without causing undesired parameter changes to other parts of the program.

TS5_COLOR ts5_set_foreground_color (const TS5_COLOR foreground_color)
TS5_COLOR ts5_get_foreground_color ()
TS5_COLOR ts5_set_background_color (const TS5_COLOR background_color)
TS5_COLOR ts5_get_background_color ()
double ts5_set_drawing_thickness (double drawing_thickness)
double ts5_get_drawing_thickness ()
int ts5_set_fill_mode (const int fill_mode)
int ts5_get_fill_mode ()
int ts5_set_font_type (const int font_type)
int ts5_get_font_type ()
int ts5_set_font_style (const int font_style)
int ts5_get_font_style ()
int ts5_set_font_size (int font_size)
int ts5_get_font_size ()
int ts5_set_font_index (const int font_index)
int ts5_get_font_index ()
int ts5_set_text_alignment (const int text_alignment)
int ts5_get_text_alignment ()

Detailed Description

Definition in file graphics.c.


Function Documentation

int ts5_set_coordinate_system ( const int  coordinate_system)

Set the coordinate system.

Parameters:
coordinate_systemCoordinate system to be used. Can be TS5_CARTESIAN_COORDINATES and TS5_DISPLAY_COORDINATES. Default is TS5_CARTESIAN_COORDINATES.
Returns:
The previous coordinate system.

The TS5_DISPLAY_COORDINATES system has the origin (0,0) in the upper left corner of the display and increasing coordinate values to the left of and below the origin.

The TS5_CARTESIAN_COORDINATES system has the origin (0,0) in the center of the display and increasing coordinate values to the left of and above the origin, and decreasing (negative) values to the right of and below the origin. This is the default coordinate system.

Examples:
bitmaps3.c, bitmaps4.c, graphics3.c, graphics4.c, primitives3.c, primitives4.c, randomizer1.c, randomizer2.c, randomizer3.c, and textio4.c.

Definition at line 60 of file graphics.c.

int ts5_get_coordinate_system ( )

Get the coordinate system.

Returns:
The coordinate system.

Definition at line 86 of file graphics.c.

int ts5_set_coordinate_scale ( const int  coordinate_scale)

Set the coordinate scale.

Parameters:
coordinate_scaleCoordinate scale to be used. Can be TS5_RELATIVE_COORDINATES and TS5_ABSOLUTE_COORDINATES. Default is TS5_ABSOLUTE_COORDINATES.
Returns:
The previous coordinate scale.

In the TS5_ABSOLUTE_COORDINATES scale coordinates are absolute pixel values. This is the default coordinate scale.

In the TS5_RELATIVE_COORDINATES scale coordinates vary from -1.0 to 1.0 in the TS5_CARTESIAN_COORDINATES system en from 0.0 to 1.0 in the TS5_DISPLAY_COORDINATES system.

Using the TS5_RELATIVE_COORDINATES scale has an influence on position but also on sizes.

Circle diameters are computed relative to the x axis.

Font size and drawing thickness are always set in pixels.

Examples:
bitmaps2.c, bitmaps4.c, graphics10.c, graphics2.c, graphics4.c, graphics5.c, graphics6.c, graphics7.c, graphics8.c, graphics9.c, primitives2.c, and primitives4.c.

Definition at line 112 of file graphics.c.

int ts5_get_coordinate_scale ( )

Get the coordinate scale.

Returns:
The coordinate scale.

Definition at line 137 of file graphics.c.

void ts5_get_target_size ( double *  w,
double *  h 
)

Get the total size of the drawing target in pixels.

Parameters:
wVariable that will store the width.
hVariable that will store the height.

The drawing target can be either a display or a memory bitmap.

You can pass NULL for values you are not interested in.

Definition at line 156 of file graphics.c.

double ts5_get_target_width ( )

Get the total width of the drawing target in pixels.

Returns:
the width of the drawing target.

The drawing target can be either a display or a memory bitmap.

Definition at line 189 of file graphics.c.

double ts5_get_target_height ( )

Get the total height of the drawing target in pixels.

Returns:
the height of the drawing target.

The drawing target can be either a display or a memory bitmap.

Definition at line 208 of file graphics.c.

double ts5_relative_to_absolute_coordinate_x ( const double  x)

Convert a relative horizontal coordinate into an absolute horizontal coordinate.

This function can be called using the macro ax(x)

Parameters:
xHorizontal coordinate value as a proportion.
Returns:
Horizontal coordinate value in pixels.

Its behavior depends on the coordinate system used.

  • With the Cartesian system x=0 refers to the center of the display, x=1 refers the to right-hand side border and x=-1 refers to the left-hand side border.
  • With the display system x=0 refers to the right-hand side border, x=1 refers to the left-hand side border.

It is possible to call this function as a parameter to a drawing function (e.g.: when using the Cartesian coordinate system, ts5_putpixel (ts5_relative_to_absolute_coordinate_x(.5), 0); writes a pixel eighty pixels to the right of the center in the case of a 320x240 sized display, or 160 pixels to the right in the case of a 640x480 sized display).

Examples:
randomizer1.c, randomizer2.c, randomizer3.c, system1.c, textio2.c, textio3.c, textio4.c, textio5.c, timer1.c, timer2.c, timer3.c, timer4.c, timer5.c, and timer6.c.

Definition at line 240 of file graphics.c.

double ts5_relative_to_absolute_coordinate_y ( const double  y)

Convert a relative vertical coordinate into an absolute vertical coordinate.

This function can be called using the macro ay(y)

Parameters:
yVertical coordinate value as a proportion.
Returns:
Vertical coordinate value in pixels.

Its behaviour completely parallells that of ts5_relative_to_absolute_coordinate_x().

Examples:
randomizer1.c, randomizer2.c, randomizer3.c, system1.c, textio2.c, textio3.c, textio4.c, textio5.c, timer1.c, timer2.c, timer3.c, timer4.c, timer5.c, and timer6.c.

Definition at line 269 of file graphics.c.

double ts5_absolute_to_relative_coordinate_x ( const double  x)

Convert an absolute horizontal coordinate into a relative horizontal coordinate.

Parameters:
xHorizontal coordinate value in pixels.
Returns:
Horizontal coordinate value as a proportion.

Mainly for internal use by Tscope5 itself.

Definition at line 295 of file graphics.c.

double ts5_absolute_to_relative_coordinate_y ( const double  y)

Convert an absolute vertical coordinate into a relative vertical coordinate.

Parameters:
yVertical coordinate value in pixels.
Returns:
Vertical coordinate value as a proportion.

Mainly for internal use by Tscope5 itself.

Definition at line 322 of file graphics.c.

double ts5_cartesian_to_display_coordinate_x ( const double  x)

Convert a horizontal Cartesian coordinate into a horizontal display coordinate.

Parameters:
xHorizontal coordinate value in the Cartesian coordinate system.
Returns:
The coordinate value in the display coordinate system.

Mainly for internal use by Tscope5 itself.

Definition at line 349 of file graphics.c.

double ts5_cartesian_to_display_coordinate_y ( const double  y)

Convert a vertical Cartesian coordinate into a vertical display coordinate.

Parameters:
yVertical coordinate value in the Cartesian coordinate system.
Returns:
The coordinate value in the display coordinate system.

Mainly for internal use by Tscope5 itself.

Definition at line 367 of file graphics.c.

double ts5_display_to_cartesian_coordinate_x ( const double  x)

Convert a horizontal display coordinate into a horizontal Cartisian coordinate.

Parameters:
xHorizontal coordinate value in the display coordinate system.
Returns:
The coordinate value in the Cartesian coordinate system.

Mainly for internal use by Tscope5 itself.

Definition at line 385 of file graphics.c.

double ts5_display_to_cartesian_coordinate_y ( const double  y)

Convert a vertical display coordinate into a vertical Cartisian coordinate.

Parameters:
yVertical coordinate value in the display coordinate system.
Returns:
The coordinate value in the Cartesian coordinate system.

Mainly for internal use by Tscope5 itself.

Definition at line 403 of file graphics.c.

TS5_COLOR ts5_make_rgb_color ( const double  r,
const double  g,
const double  b,
const double  a 
)

Generate a color specification based on an RGB triplet plus an alpha channel.

Parameters:
rRed. Range from 0.0 to 1.0.
gGreen. Range from 0.0 to 1.0.
bBlue. Range from 0.0 to 1.0.
aAlpha. 0.0 is completely transparant and 1.0 is completely opaque.
Returns:
TS5_COLOR structure.
Examples:
graphics5.c.

Definition at line 442 of file graphics.c.

TS5_COLOR ts5_make_hsl_color ( const double  h,
const double  s,
const double  l,
const double  a 
)

Generate a color specification based on an HSL triplet plus an alpha channel.

Parameters:
hHue. Range from 0.0 to 360.0.
sSaturation. Range from 0.0 to 1.0.
lLightness. Range from 0.0 to 1.0.
aAlpha. 0.0 is completely transparant and 1.0 is completely opaque.
Returns:
TS5_COLOR sctructure.
Examples:
graphics6.c.

Definition at line 465 of file graphics.c.

TS5_COLOR ts5_make_hsv_color ( const double  h,
const double  s,
const double  v,
const double  a 
)

Generate a color specification based on an HSV triplet plus an alpha channel.

Parameters:
hHue. Range from 0.0 to 360.0.
sSaturation. Range from 0.0 to 1.0.
vValue Range from 0.0 to 1.0.
aAlpha. 0.0 is completely transparant and 1.0 is completely opaque.
Returns:
TS5_COLOR sctructure.
Examples:
graphics7.c.

Definition at line 491 of file graphics.c.

TS5_COLOR ts5_make_named_color ( const char *  name,
const double  a 
)

Return a color that corresponds to name.

Parameters:
nameName of the color.
aAlpha. 0.0 is completely transparant and 1.0 is completely opaque.
Returns:
TS5_COLOR sctructure.

There recognized names are: aliceblue, antiquewhite, aqua, aquamarine, azure, beige, bisque, black, blanchedalmond, blue, blueviolet, brown, burlywood, cadetblue, chartreuse, chocolate, coral, cornflowerblue, cornsilk, crimson, cyan, darkblue, darkcyan, darkgoldenrod, darkgray, darkgreen, darkkhaki, darkmagenta, darkolivegreen, darkorange, darkorchid, darkred, darksalmon, darkseagreen, darkslateblue, darkslategray, darkturquoise, darkviolet, deeppink, deepskyblue, dimgray, dodgerblue, firebrick, floralwhite, forestgreen, fuchsia, gainsboro, ghostwhite, goldenrod, gold, gray, green, greenyellow, honeydew, hotpink, indianred, indigo, ivory, khaki, lavenderblush, lavender, lawngreen, lemonchiffon, lightblue, lightcoral, lightcyan, lightgoldenrodyellow, lightgreen, lightgrey, lightpink, lightsalmon, lightseagreen, lightskyblue, lightslategray, lightsteelblue, lightyellow, lime, limegreen, linen, magenta, maroon, mediumaquamarine, mediumblue, mediumorchid, mediumpurple, mediumseagreen, mediumslateblue, mediumspringgreen, mediumturquoise, mediumvioletred, midnightblue, mintcream, mistyrose, moccasin, avajowhite, navy, oldlace, olive, olivedrab, orange, orangered, orchid, palegoldenrod, palegreen, paleturquoise, palevioletred, papayawhip, peachpuff, peru, pink, plum, powderblue, purple, purwablue, red, rosybrown, royalblue, saddlebrown, salmon, sandybrown, seagreen, seashell, sienna, silver, skyblue, slateblue, slategray, snow, springgreen, steelblue, tan, teal, thistle, tomato, turquoise, violet, wheat, white, whitesmoke, yellow and yellowgreen.

Colors are taken from http://www.w3.org/TR/2010/PR-css3-color-20101028/#svg-color.

If the color is not found then black is returned.

Examples:
bitmaps1.c, bitmaps2.c, bitmaps3.c, bitmaps4.c, graphics1.c, graphics2.c, graphics3.c, graphics4.c, graphics8.c, and graphics9.c.

Definition at line 537 of file graphics.c.

TS5_COLOR ts5_set_foreground_color ( const TS5_COLOR  foreground_color)

Set the foreground color.

Parameters:
foreground_colorTS5_COLOR color specification.
Returns:
The previous foreground color.

Changes the drawing color of all subsequent drawing operations. The default color is white. Once a color is set, it remains active until another color is set.

Tscope5 represents colors in an opaque structure (TS5_COLOR). Several functions exist for generating such a color definition (see above).

Examples:
graphics1.c, graphics2.c, graphics3.c, graphics4.c, graphics5.c, graphics6.c, graphics7.c, graphics8.c, and graphics9.c.

Definition at line 590 of file graphics.c.

TS5_COLOR ts5_get_foreground_color ( )

Get the foreground color.

Returns:
The foreground color.

Definition at line 614 of file graphics.c.

TS5_COLOR ts5_set_background_color ( const TS5_COLOR  background_color)

Set the background color.

Parameters:
background_colorTS5_COLOR color specification.
Returns:
The previous background color.

Changes the background color of all new drawing targets or when a drawing target is cleared.

The default color is black. Once a color is set, it remains active until another color is set.

Examples:
graphics1.c, graphics2.c, graphics3.c, graphics4.c, and graphics9.c.

Definition at line 634 of file graphics.c.

TS5_COLOR ts5_get_background_color ( )

Get the background color.

Returns:
The background color.

Definition at line 658 of file graphics.c.

double ts5_set_drawing_thickness ( double  drawing_thickness)

Set the drawing thickness.

Parameters:
drawing_thicknessDrawing thickness in pixels.
Returns:
The previous thickness.

Changes thickness of lines for all drawing operations.

The default thickness is 0 (hairline).

Examples:
graphics9.c.

Definition at line 678 of file graphics.c.

double ts5_get_drawing_thickness ( )

Get the drawing thickness.

Returns:
The drawing thickness.

Definition at line 703 of file graphics.c.

int ts5_set_fill_mode ( const int  fill_mode)

Set the fill mode.

Parameters:
fill_modeFill mode. Can be TS5_FILL_ON or TS5_FILL_OFF.
Returns:
The previous fill mode.

Changes the fill mode for future drawing operations.

The default fill mode is TS5_FILL_OFF.

Examples:
graphics5.c, graphics6.c, graphics7.c, and graphics8.c.

Definition at line 723 of file graphics.c.

int ts5_get_fill_mode ( )

Get the fill mode.

Returns:
The fill mode.

Definition at line 748 of file graphics.c.

int ts5_set_font_type ( const int  font_type)

Set the font type.

Parameters:
font_typeFont type. Can be TS5_COURIER, TS5_ARIAL and TS5_TIMES.
Returns:
The previous font type.

The default font type is TS5_COURIER.

Examples:
graphics10.c, and textio3.c.

Definition at line 766 of file graphics.c.

int ts5_get_font_type ( )

Get the font type.

Returns:
The font type.

Definition at line 796 of file graphics.c.

int ts5_set_font_style ( const int  font_style)

Set the font style.

Parameters:
font_styleFont style. Can be TS5_REGULAR, TS5_BOLD, TS5_ITALIC or TS5_BOLD_ITALIC.
Returns:
The previous font style.

The default font style is TS5_BOLD.

Examples:
graphics10.c, and textio3.c.

Definition at line 814 of file graphics.c.

int ts5_get_font_style ( )

Get the font style.

Returns:
The font style.

Definition at line 846 of file graphics.c.

int ts5_set_font_size ( int  font_size)

Set the font size.

Parameters:
font_sizeFont size. Can be any value between 8 and 107.
Returns:
The previous font size.

The default font size is 20.

Examples:
graphics10.c, and textio3.c.

Definition at line 864 of file graphics.c.

int ts5_get_font_size ( )

Get the font size.

Returns:
The font size.
Examples:
textio3.c.

Definition at line 894 of file graphics.c.

int ts5_set_font_index ( const int  font_index)

Set the font index.

Parameters:
font_indexFont index. Can be any integer between 0 and TS5_NFONTS-1.
Returns:
The previous font index.

Sets the font type, style and width at once.

Definition at line 913 of file graphics.c.

int ts5_get_font_index ( )

Get the font index.

Returns:
The font index.

Definition at line 943 of file graphics.c.

int ts5_set_text_alignment ( const int  text_alignment)

Set the alignment of text.

Parameters:
text_alignmentText alignment. Can be TS5_ALIGN_LEFT, TS5_ALIGN_CENTER or TS5_ALIGN_RIGHT.
Returns:
The previous alignment setting.

The default aligment is TS5_ALIGN_CENTER.

Examples:
randomizer1.c, randomizer2.c, randomizer3.c, textio1.c, textio4.c, and timer4.c.

Definition at line 961 of file graphics.c.

int ts5_get_text_alignment ( )

Get the alignment of text.

Returns:
The alignment setting.

Definition at line 989 of file graphics.c.