File: matrix.c

package info (click to toggle)
staden 2.0.0%2Bb11-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 21,584 kB
  • sloc: ansic: 240,605; tcl: 65,360; cpp: 12,854; makefile: 11,203; sh: 3,023; fortran: 2,033; perl: 63; awk: 46
file content (123 lines) | stat: -rw-r--r-- 2,165 bytes parent folder | download | duplicates (5)
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include <stdio.h>
#include <math.h>

#include "matrix.h"

void make_coord(m_coords *point, 
		double x, 
		double y) 
{
    point->x = x;
    point->y = y;
    point->w = 1.0;
}

void print_coords(m_coords *coords[], 
		  int num) {
    int i;
    for (i = 0; i < num; i++) {
	printf("%d %f %f\n", i, coords[i]->x, coords[i]->y);
    }
}

void make_zoom_matrix(double M[3][3], 
		      ZoomValues *z) 
{  
    M[0][0] = z->scale_x;
    M[0][1] = 0.0;
    M[0][2] = z->origin_x * (1 - z->scale_x);
    M[1][0] = 0.0;
    M[1][1] = z->scale_y;
    M[1][2] = z->origin_y * (1 - z->scale_y);
    M[2][0] = M[2][1] = 0.0;
    M[2][2] = 1.0;
}

void make_identity_matrix(double I[3][3]) 
{
  int i,j;

  for(i = 0; i < 3; i++) {
      for(j = 0; j < 3; j++) {
	  I[i][j] = 0.0;
      }
  }
  I[0][0] = I[1][1] = I[2][2] = 1.0;
}

void make_rotation_matrix(double M[3][3],
			  double angle)
{

    M[0][0] = cos(angle);
    M[0][1] = -1 * sin(angle);
    M[0][2] = 0.0;
    M[1][0] = sin(angle);
    M[1][1] = cos(angle);
    M[1][2] = 0.0;
    M[2][0] = 0.0;
    M[2][1] = 0.0;
    M[2][2] = 1.0;
}

void make_translation_matrix(double M[3][3],
			     double x,
			     double y)
{

    M[0][0] = 1.0;
    M[0][1] = 0.0;
    M[0][2] = x;
    M[1][0] = 0.0;
    M[1][1] = 1.0;
    M[1][2] = y;
    M[2][0] = 0.0;
    M[2][1] = 0.0;
    M[2][2] = 1.0;
}

void copy_matrix(double to[3][3], 
		 double from[3][3]) 
{
    int i, j;

    for(i = 0; i < 3; i++) {
	for(j = 0; j < 3; j++) {
	    to[i][j] = from[i][j];
	}
    }
}

/*
 * multiply B x A
 */
void multiply_matrices(double out[3][3], 
		       double b[3][3], 
		       double a[3][3]) 
{
    int i,j,k;
    
    for (i = 0; i < 2; i++) {
	for (j = 0; j < 3; j++) {
	    out[i][j] = 0.0;
	    for (k = 0; k < 3; k++) {
		out[i][j] = out[i][j] + b[i][k] * a[k][j];
	    }
	}
    }
    out[2][0] = out[2][1] = 0.0;
    out[2][2] = 1.0;
}

/*
 * multiply COORDS x Matrix
 */
void multiply_coords_by_matrix(m_coords *out, 
			       double m[3][3], 
			       m_coords *in) 
{
    
    out->x = m[0][0] * in->x + m[0][1] * in->y + m[0][2] * in->w;
    out->y = m[1][0] * in->x + m[1][1] * in->y + m[1][2] * in->w;
}