Root/
| 1 | /* |
| 2 | * gfx/misc.c - Helper functions for geometry and attributes |
| 3 | * |
| 4 | * Written 2016 by Werner Almesberger |
| 5 | * Copyright 2016 Werner Almesberger |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | */ |
| 12 | |
| 13 | |
| 14 | #include <stdbool.h> |
| 15 | #include <stdlib.h> |
| 16 | #include <stdio.h> |
| 17 | #include <assert.h> |
| 18 | |
| 19 | #include "misc/diag.h" |
| 20 | #include "gfx/misc.h" |
| 21 | |
| 22 | |
| 23 | static bool eq(const int m[6], int xx, int xy, int yx, int yy) |
| 24 | { |
| 25 | return m[1] == xx && m[2] == xy && m[4] == yx && m[5] == yy; |
| 26 | } |
| 27 | |
| 28 | |
| 29 | unsigned matrix_to_angle(const int m[6]) |
| 30 | { |
| 31 | if (eq(m, 1, 0, 0, -1)) |
| 32 | return 0; |
| 33 | if (eq(m, 0, -1, -1, 0)) |
| 34 | return 90; |
| 35 | if (eq(m, -1, 0, 0, 1)) |
| 36 | return 180; |
| 37 | if (eq(m, 0, 1, 1, 0)) |
| 38 | return 270; |
| 39 | |
| 40 | if (eq(m, -1, 0, 0, -1)) |
| 41 | return 0; |
| 42 | if (eq(m, 0, 1, -1, 0)) /* x-flipped, 90 deg */ |
| 43 | return 90; |
| 44 | if (eq(m, 1, 0, 0, 1)) /* x-flipped */ |
| 45 | return 180; |
| 46 | if (eq(m, 0, -1, 1, 0)) /* x-flipped, 270 deg */ |
| 47 | return 270; |
| 48 | |
| 49 | fatal("unrecognized matrix %d %d %d %d\n", m[1], m[2], m[4], m[5]); |
| 50 | } |
| 51 | |
| 52 | |
| 53 | bool matrix_is_mirrored(const int m[6]) |
| 54 | { |
| 55 | if (eq(m, 1, 0, 0, -1)) |
| 56 | return 0; |
| 57 | if (eq(m, 0, -1, -1, 0)) |
| 58 | return 0; |
| 59 | if (eq(m, -1, 0, 0, 1)) |
| 60 | return 0; |
| 61 | if (eq(m, 0, 1, 1, 0)) |
| 62 | return 0; |
| 63 | |
| 64 | if (eq(m, -1, 0, 0, -1)) |
| 65 | return 1; |
| 66 | if (eq(m, 0, 1, -1, 0)) |
| 67 | return 1; |
| 68 | if (eq(m, 1, 0, 0, 1)) |
| 69 | return 1; |
| 70 | if (eq(m, 0, -1, 1, 0)) |
| 71 | return 1; |
| 72 | |
| 73 | assert(0); |
| 74 | } |
| 75 | |
| 76 | |
| 77 | int angle_add(int a, int b) |
| 78 | { |
| 79 | a += b; |
| 80 | while (a < 0) |
| 81 | a += 360; |
| 82 | return a % 360; |
| 83 | } |
| 84 | |
| 85 | |
| 86 | |
| 87 | int rx(int x, int y, int rot) |
| 88 | { |
| 89 | switch (rot) { |
| 90 | case 0: |
| 91 | return x; |
| 92 | case 90: |
| 93 | return y; |
| 94 | case 180: |
| 95 | return -x; |
| 96 | case 270: |
| 97 | return -y; |
| 98 | default: |
| 99 | assert(0); |
| 100 | |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | |
| 105 | int ry(int x, int y, int rot) |
| 106 | { |
| 107 | switch (rot) { |
| 108 | case 0: |
| 109 | return y; |
| 110 | case 90: |
| 111 | return -x; |
| 112 | case 180: |
| 113 | return -y; |
| 114 | case 270: |
| 115 | return x; |
| 116 | default: |
| 117 | assert(0); |
| 118 | |
| 119 | } |
| 120 | } |
| 121 |
Branches:
master
