summaryrefslogtreecommitdiff
path: root/ellipse/main.py
blob: 93af34e9e258931c394f037f07907b7578c0f390 (plain)
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
from pygame.locals import *
import pygame
from pygame.math import Vector2

import math
from math import sin, cos, pi, sqrt
import time


def screen2local(x, y):
    nx = x - WIDTH / 2
    ny = HEIGHT / 2 - y
    return (nx, ny)


def local2screen(x, y):
    nx = x + WIDTH / 2
    ny = HEIGHT / 2 - y
    return (nx, ny)


class Ellipse:
    def __init__(self, a, b):
        self.a = a
        self.b = b

        self.c = sqrt(self.a * self.a - self.b * self.b)
        #  self.f1 = local2screen(self.c, 0)
        #  self.f2 = local2screen(-self.c, 0)
        self.f1 = (self.c, 0)
        self.f2 = (-self.c, 0)

    def draw(self, deltatime, screen, cx, cy):
        cnt = 0
        for i in range(0, int(2 * pi * 100) + 1):
            cnt += 1
            t = i / 100
            t1 = t + 1 / 100

            # Координаты в локальном пространстве
            x = self.a * cos(t)
            y = self.b * sin(t)
            x1 = self.a * cos(t1)
            y1 = self.b * sin(t1)

            start = local2screen(x, y)
            end = local2screen(x1, y1)

            pygame.draw.line(screen, WHITE, start, end, 2)

        pygame.draw.circle(screen, WHITE, local2screen(*self.f1), 3)
        pygame.draw.circle(screen, WHITE, local2screen(*self.f2), 3)


class NiceEllipse:
    def __init__(self, a, b):
        self.a = a
        self.b = b

        self.time = 0
        self.timeout = 700
        self.i = 0
        self.lines = []
        
        self.c = sqrt(self.a * self.a - self.b * self.b)
        self.f1 = local2screen(self.c, 0)
        self.f2 = local2screen(-self.c, 0)

        self.lastend = None

    def draw(self, deltatime, screen, cx, cy):
        print(deltatime)
        for start, end in self.lines:
            pygame.draw.line(screen, WHITE, start, end, 2)

        pygame.draw.circle(screen, WHITE, self.f1, 3)
        pygame.draw.circle(screen, WHITE, self.f2, 3)

        if self.lastend is not None:
            pygame.draw.line(screen, RED, self.f1, self.lastend, 2)
            pygame.draw.line(screen, GREEN, self.f2, self.lastend, 2)

        if self.i >= int(2 * pi * 100) + 1:
            self.lastend = None
            return

        if self.time > 0:
            self.time -= deltatime
            return

        self.time = self.timeout

        t = self.i / 100
        t1 = t + 1 / 100
        self.i += 1

        # Координаты в локальном пространстве
        x = self.a * cos(t)
        y = self.b * sin(t)
        x1 = self.a * cos(t1)
        y1 = self.b * sin(t1)

        start = local2screen(x, y)
        end = local2screen(x1, y1)
        self.lastend = end

        self.lines.append((start, end))

        pygame.draw.line(screen, WHITE, start, end, 2)


class ScreenRay:
    def __init__(self, a: Vector2, b: Vector2, speed: float):
        #  self.a = a
        #  self.b = b
        self.pos = a
        self.dir = Vector2(b.x - a.x, b.y - a.y)
        self.dir.normalize_ip()
        self.speed = speed

    def update(self, deltatime):
        n_pos = self.pos + self.dir * (deltatime * self.speed)
        
        scr = Vector2(*local2screen(*tuple(self.pos)))
        scr_npos = Vector2(*local2screen(*tuple(n_pos)))

        if scr_npos.x < 0:
            self.dir = Vector2(-self.dir.x, self.dir.y)
            n_pos = Vector2(*screen2local(0, scr.y))
        elif scr_npos.x > WIDTH:
            self.dir = Vector2(-self.dir.x, self.dir.y)
            n_pos = Vector2(*screen2local(WIDTH, scr.y))
        elif scr_npos.y < 0:
            self.dir = Vector2(self.dir.x, -self.dir.y)
            n_pos = Vector2(*screen2local(scr.x, 0))
        elif scr_npos.y > HEIGHT:
            self.dir = Vector2(self.dir.x, -self.dir.y)
            n_pos = Vector2(*screen2local(scr.x, HEIGHT))

        self.pos = n_pos


class EllipseRay:
    def __init__(self, s: Vector2, e: Vector2, speed: float, el):
        self.a = el.a
        self.b = el.b
        self.pos = e
        self.dir = Vector2(e.x - s.x, e.y - s.y)
        self.dir.normalize_ip()
        self.speed = speed

    def eps(self, x, y):
        a = self.a
        b = self.b
        tmp = (x * x) / (a * a) + (y * y) / (b * b)
        e = 0.05
        return tmp < 1 + e and tmp > 1 - e

    def update(self, deltatime):
        npos = self.pos + self.dir * (deltatime * self.speed)
        
        scr = Vector2(*local2screen(*tuple(self.pos)))
        scr_npos = Vector2(*local2screen(*tuple(npos)))

        p0 = self.pos

        ########################
        # Отражение от эллипса #
        ########################

        if p0.y == 0:
            if p0.x < 0:
                self.dir.reflect_ip(Vector2(-1, 0))
            else:
                self.dir.reflect_ip(Vector2(1, 0))
        else:
            t = 1
            m = 1
            n = -(p0.x * self.b * self.b) / (p0.y * self.a * self.a)
            p1 = Vector2(p0.x + m * t, p0.y + n * t)

            kas = Vector2(p1.x - p0.x, p1.y - p0.y)
            kas.normalize_ip()
            #  angle = kas.angle_to(self.dir)

            kas_p = Vector2(1, -kas.x / kas.y)
            kas_p.normalize_ip()

            if self.eps(npos.x, npos.y):
                self.dir.reflect_ip(kas_p)

        ########################

        self.pos = npos


    def draw(self, screen):
        loc = tuple(self.pos)
        scr = local2screen(*loc)
        pygame.draw.circle(screen, RED, scr, 3)



class Ray1Ellipse:
    def __init__(self, a, b):
        self.a = a
        self.b = b

    def draw(self, deltatime, screen, cx, cy):
        cnt = 0
        for i in range(0, int(2 * pi * 100) + 1):
            cnt += 1
            t = i / 100
            t1 = t + 1 / 100

            # Координаты в локальном пространстве
            x = self.a * cos(t)
            y = self.b * sin(t)
            x1 = self.a * cos(t1)
            y1 = self.b * sin(t1)

            start = local2screen(x, y)
            end = local2screen(x1, y1)

            pygame.draw.line(screen, WHITE, start, end, 2)
        
        c = sqrt(self.a * self.a - self.b * self.b)
        f1 = local2screen(c, 0)
        f2 = local2screen(-c, 0)

        pygame.draw.circle(screen, WHITE, f1, 3)
        pygame.draw.circle(screen, WHITE, f2, 3)
        

pygame.init()

WIDTH, HEIGHT = SIZE = 800, 600

screen = pygame.display.set_mode(SIZE)

clock = pygame.time.Clock()
running = True

BLACK = Color("black")
WHITE = Color("white")
GREEN = Color("green")
RED = Color("red")

el = Ellipse(300, 200)

rays = []
nn = 10
for i in range(1, nn + 1):
    x0, y0 = el.f2
    xr = cos(2 * pi * i / nn) + x0
    yr = sin(2 * pi * i / nn) + y0
    r = EllipseRay(Vector2(x0, y0), Vector2(xr, yr), 70, el)
    rays.append(r)

#  deltatime = pygame.time.get_ticks()

while running:
    screen.fill(BLACK)

    deltatime = clock.get_time() / 1000

    for r in rays:
        r.update(deltatime)
        r.draw(screen)
    el.draw(deltatime, screen, 0, 0)

    # ...

    pygame.display.flip()
    
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            exit()
        elif event.type == KEYDOWN:
            if event.key == K_ESCAPE:
                pygame.quit()
                exit()
    clock.tick(60)
    #  deltatime = pygame.time.get_ticks() - deltatime
    #  print(clock.get_fps())