Skip to content

Instantly share code, notes, and snippets.

@automata
Created September 29, 2015 02:13
Show Gist options
  • Select an option

  • Save automata/e2eb29aefd9286ab2396 to your computer and use it in GitHub Desktop.

Select an option

Save automata/e2eb29aefd9286ab2396 to your computer and use it in GitHub Desktop.
Test rotate-on-center.c op for GEGL
#include <gegl.h>
#include <gegl/gegl-matrix.h>
#include <math.h>
static void
generate_matrix (GeglMatrix3 *matrix,
gdouble degrees,
gdouble x,
gdouble y,
gdouble width,
gdouble height)
{
gint i;
gdouble radians,
tx = 0.0,
ty = 0.0,
tcoords[4][2];
radians = degrees * (2 * G_PI / 360.0);
/*
* Find coordinates of each corner of the bounding box around buffer,
* after the requested rotation.
*/
tcoords[0][0] = - x*cos(radians) - y*sin(radians);
tcoords[0][1] = x*sin(radians) - y*cos(radians);
tcoords[1][0] = width*cos(radians) + tcoords[0][0];
tcoords[1][1] = - width*sin(radians) + tcoords[0][1];
tcoords[2][0] = width*cos(radians) + height*sin(radians) + tcoords[0][0];
tcoords[2][1] = - width*sin(radians) + height*cos(radians) + tcoords[0][1];
tcoords[3][0] = height*sin(radians) + tcoords[0][0];
tcoords[3][1] = height*cos(radians) + tcoords[0][1];
/*
* Find translation needed to make the bounding box stays in the positive
* quadrant.
*/
for (i=0; i<G_N_ELEMENTS (tcoords); i++) {
tx = MIN (tx, tcoords[i][0]);
ty = MIN (ty, tcoords[i][1]);
}
/*
* Define the affine matrix that:
* 1. Translate buffer's center to origin
* 2. Rotate buffer by given degrees
* 3. Translate to positive quadrant
*/
matrix->coeff [0][0] = cos (radians);
matrix->coeff [0][1] = sin (radians);
matrix->coeff [0][2] = -tx-x*cos(radians)-y*sin(radians);
matrix->coeff [1][0] = -sin (radians);
matrix->coeff [1][1] = cos (radians);
matrix->coeff [1][2] = -ty+x*sin(radians)-y*cos(radians);
matrix->coeff [2][0] = 0;
matrix->coeff [2][1] = 0;
matrix->coeff [2][2] = 1;
}
gint
main (gint argc,
gchar **argv)
{
GeglMatrix3 *matrix = gegl_matrix3_new();
gint degrees, i;
gdouble target_x,
target_y,
center_x,
center_y,
width = 400.0,
height = 100.0;
gdouble targets[] = {
0.0, 0.0,
width, 0.0,
width, height,
0.0, height
};
center_x = width / 2;
center_y = height / 2;
for (degrees=0; degrees<360; degrees++) {
for (i=0; i<G_N_ELEMENTS (targets); i+=2) {
target_x = targets[i];
target_y = targets[i+1];
generate_matrix(matrix, (gdouble) degrees, center_x, center_y, width, height);
g_print("%f\t%f\t=>\t", target_x, target_y);
gegl_matrix3_transform_point(matrix, &target_x, &target_y);
g_print("%f\t%f\n", target_x, target_y);
if (target_x < 0 || target_y < 0) {
g_print("Negative values at %d %f %f!\n\n", degrees, target_x, target_y);
}
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment