# Operations¶

Here are presented the functions which apply an transformation operation on a Pixel or on a Form.

## Pixel operations functions¶

This function are the base for the forms operations functions.

You can use it to implement your own operations.

Pixel rotate(Pixel center, float angle, Pixel pixel)
Parameters: center (Pixel) – The center of the rotation. angle (float) – The angle of the rotation in degress. pixel (Pixel) – The pixel to rotate. A new rotated Pixel.

This function return the rotated pixel from angle degrees around the given center in clock sens.

Note

Rotating a pixel around the origin is easy doing according following formel in matrix form:

+-  -+     +--                            --+     +- -+
| x1 |     | cos(angle)   -sin(angle)    0  |     | x |
| y2 |  =  | sin(angle)    cos(angle)    0  |  *  | y |
| 0  |     |    0             0          1  |     | 0 |
+-  -+     +--                            --+     +- -+


So you can simply translate the pixel in accord to the origin rotate it and translate it back.

But SDL2_gfxutils use a function based on matrix to rotate the pixel around an arbitrary point.

See the source at file base_functions.c.

Pixel scale(Pixel center, float factor, Pixel pixel)
Parameters: center (Pixel) – The center of the form to scale. factor (float) – The scaling factor. pixel (Pixel) – The pixel to scale. A new scaled pixel (position).

This function return the new position from pixel scaled by factor:

factor < 1 == scaling littler.

factor > 1 == scaling greater.


Note

A pixel can only be corrected scaled in accord to the origin.

So you can simply translate the pixel in accord to the origin multiply x and y with the scaling factor and translate it back.

Pixel translate(Pixel pixel, float x, float y)
Parameters: pixel (Pixel) – The pixel to translate. x (float) – The translation value from the x axes (even negativ). y (float) – The translation value from the y axes (even negativ). A new pixel translated from x and y.

This function translate a pixel from value x and y.

Note: x and y can be negativ for translating in direction of the left or to the top.

Note

For translating a pixel simply add (even negativ) the wanted values to the x and y from the pixel Pixel members.

Pixel mirror(Pixel pixel, Pixel center, char axes)
Parameters: pixel (Pixel) – The pixel to mirror. center (Pixel) – The center of the mirroring. axes (char) – ‘X’ or ‘Y’. A new pixel mirrored around center trough the X or Y axes.

This function mirror a pixel through the x (Vertical) or y (Horizontal) axes in relationship to the given center.

Note

The center of the mirroring.

The center argument given the mirroring center and in case of mirroring on the:

• X axes only the x of the Pixel counting.
• Y axes only the y of the Pixel counting.

Warning

Take care of the Form operation function condition.

### Operations on a pixel according to the origin¶

x += -center.x ; y += -center.y  // (translate according the origin).

// Operation on the pixel.

x += center.x ; y += center.y  // (translate it back).


## Forms operations functions¶

They all apply a transformation on a form by calling the pixels operations functions.

By using a pointer on the generic Form type form to transform given as argument.

void rotate_form(Form *form, float angle)
Parameters: form (Form) – A pointer on the form to rotate. angle (float) – The angle of the rotation. void.

This function perform a rotation on a form itself, through an pointer on it, from angle degrees around the center from the form.

Note

Rotation center.

You can change temporary the center of the form you want to rotate the form around the wanted center,

with the function set_form_center().

instead of the center of the form itself.

warning: If you use a display function which strikethrough from the center: the displaying will degenerate (maybe you do it express).
void scale_form(Form *form, float factor)
Parameters: form (Form) – A pointer on the form to scale. factor (float) – The scaling factor. void.

This function scale the adressed form from value factor.

Note

Scaling factor.

• if factor > 1.0 the size of the form increase.
• if factor < 1.0 the size from the form decrease.

Note

You can set a new radius (which will update the length Form type member) directly,

What permit to change the size of the form without using a factor but a radius instead.

warning: Use only integers values or not more than 3 precision (%.3f) otherwise your request will not be exactly satisfy.
void translate_form(Form *form, float x, float y)
Parameters: form (Form) – A pointer on the form to translate. x (float) – The translation value from the x axes (even negativ). y (float) – The translation value from the y axes (even negativ). void.

This function translate the adressed form from values x and y.

Note: x and y can be negativ for translating in direction of the left or to the top. Use only integers values or not more than 3 precision (%.3f) otherwise your request will not be exactly satisfy.
void mirror_form(Form *form, Pixel center, char axes)
Parameters: form (Pixel) – A pointer on the form to mirror. center – The center for the mirroring. axes (char) – ‘X’ or ‘Y’. void.

This function mirror the given form through the x (Vertical) or y (Horizontal) axes in relationship to the given center.

Warning

This function is subject of a big condition to work properly !!!

All coordinates must be at one side from the center axe.

Argument axes:

‘X’) If mirroring over axes X all pixels must must be above or below from the center argument x Pixel type member.

‘Y’) If mirroring over axes Y all pixels must must be at the right or at the left from the center argument y Pixel type member.

Form *remove_doubles_form(Form *form) ;
Parameters:
• form (Form) – A pointer on the form to mirror.
Return type:

Form *

Returns:

The same form with doubles (same values) coordinates removed.

note: The given form is free and reallocated (sorry can’t do otherwise).