File: spirals.c

package info (click to toggle)
lebiniou 3.67.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,500 kB
  • sloc: ansic: 28,670; makefile: 1,276; sh: 602; awk: 432; xml: 261; javascript: 23
file content (137 lines) | stat: -rw-r--r-- 2,664 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
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
 *  Copyright 1994-2022 Olivier Girondel
 *  Copyright 2014-2022 Frantz Balinski
 *
 *  This file is part of lebiniou.
 *
 *  lebiniou 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.
 *
 *  lebiniou 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 lebiniou. If not, see <http://www.gnu.org/licenses/>.
 */

#include "brandom.h"
#include "context.h"
#include "translation.h"
#include "point2d.h"


uint32_t version = 0;
uint32_t options = BO_DISPLACE;
char desc[] = "Poly-spirals effect";
char dname[] = "Spirals";

static Translation_t *t_spiral = NULL;


#define NUMCENTERS  (16)

static Point2d_t  centers[NUMCENTERS];


#define DELTA_ANGLE  (M_PI_4)


static void init_params(void);
static Map_t cth_spiral(const short, const short);

static inline float
hypof(const float x, const float y)
{
  return sqrtf(x*x + y*y);
}


int8_t
create(Context_t *ctx)
{
  t_spiral = Translation_new(&cth_spiral, &init_params);

  return 1;
}


void
destroy(Context_t *ctx)
{
  if (NULL != t_spiral) {
    Translation_delete(t_spiral);
  }
}


void
run(Context_t *ctx)
{
  Translation_run(t_spiral, ctx);
}


void
on_switch_on(Context_t *ctx)
{
  Translation_batch_init(t_spiral);
}


static void
init_params(void)
{
  for (int16_t i = 0; i < NUMCENTERS; i++) {
    centers[i].x = b_rand_uint32_range(MINX, MAXX + 1);
    centers[i].y = b_rand_uint32_range(MINY, MAXY + 1);
  }
}


static Map_t
cth_spiral(const short xx, const short yy)
{
  Map_t m;
  float dx, dy;
  float radius = hypof(WIDTH >> 2, HEIGHT >> 2);
  float x = xx;
  float y = yy;

  for (int16_t i = 0; i < NUMCENTERS; i++) {
    float cx = centers[i].x;
    float cy = centers[i].y;

    if ((y == MINY) || (y == MAXY)) {
      dx = ((cx - x) * 3) / 4;
      dy = cy - y;
    } else if ((x == MINX) || (x == MAXX)) {
      dx = cx - x;
      dy = ((cy - y) * 3) / 4;
    } else {
      dx = x - cx;
      dy = y - cy;

      float dist = hypof(dx, dy);
      if (dist < radius) {
        float ang = atan2f(dy, dx) - DELTA_ANGLE;
        dist /= 10;
        dx = -sinf(ang) * dist;
        dy =  cosf(ang) * dist;
      } else {
        dx = dy = 0;
      }
    }

    x += dx;
    y += dy;
  }

  m.map_x = x;
  m.map_y = y;

  return m;
}