File: affine.c

package info (click to toggle)
libdc1394 1.1.0-3
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 1,748 kB
  • ctags: 726
  • sloc: sh: 8,075; ansic: 6,903; makefile: 84
file content (88 lines) | stat: -rw-r--r-- 2,808 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/*
 * affine.c -- Affine Transforms for 2d objects
 * Copyright (C) 2002 Charles Yates <charles.yates@pandora.be>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include "affine.h"

static inline void Multiply( affine_transform_t *this, affine_transform_t *that )
{
	double output[2][2];
	register int i, j;

	for ( i = 0; i < 2; i ++ )
		for ( j = 0; j < 2; j ++ )
			output[ i ][ j ] = this->matrix[ i ][ 0 ] * that->matrix[ j ][ 0 ] +
							   this->matrix[ i ][ 1 ] * that->matrix[ j ][ 1 ];

	this->matrix[ 0 ][ 0 ] = output[ 0 ][ 0 ];
	this->matrix[ 0 ][ 1 ] = output[ 0 ][ 1 ];
	this->matrix[ 1 ][ 0 ] = output[ 1 ][ 0 ];
	this->matrix[ 1 ][ 1 ] = output[ 1 ][ 1 ];
}

void affine_transform_init( affine_transform_t *this )
{
	this->matrix[ 0 ][ 0 ] = 1;
	this->matrix[ 0 ][ 1 ] = 0;
	this->matrix[ 1 ][ 0 ] = 0;
	this->matrix[ 1 ][ 1 ] = 1;
}

// Rotate by a given angle
void affine_transform_rotate( affine_transform_t *this, double angle )
{
	affine_transform_t affine;
	affine.matrix[ 0 ][ 0 ] = cos( angle * M_PI / 180 );
	affine.matrix[ 0 ][ 1 ] = 0 - sin( angle * M_PI / 180 );
	affine.matrix[ 1 ][ 0 ] = sin( angle * M_PI / 180 );
	affine.matrix[ 1 ][ 1 ] = cos( angle * M_PI / 180 );
	Multiply( this, &affine );
}

// Shear by a given value
void affine_transform_shear( affine_transform_t *this, double shear )
{
	affine_transform_t affine;
	affine.matrix[ 0 ][ 0 ] = 1;
	affine.matrix[ 0 ][ 1 ] = shear;
	affine.matrix[ 1 ][ 0 ] = 0;
	affine.matrix[ 1 ][ 1 ] = 1;
	Multiply( this, &affine );
}

void affine_transform_scale( affine_transform_t *this, double sx, double sy )
{
	affine_transform_t affine;
	affine.matrix[ 0 ][ 0 ] = sx;
	affine.matrix[ 0 ][ 1 ] = 0;
	affine.matrix[ 1 ][ 0 ] = 0;
	affine.matrix[ 1 ][ 1 ] = sy;
	Multiply( this, &affine );
}

// Obtain the mapped x coordinate of the input
double affine_transform_mapx( affine_transform_t *this, int x, int y )
{
	return this->matrix[0][0] * x + this->matrix[0][1] * y;
}

// Obtain the mapped y coordinate of the input
double affine_transform_mapy( affine_transform_t *this, int x, int y )
{
	return this->matrix[1][0] * x + this->matrix[1][1] * y;
}