1
1
from Ledart import Graphics , BLACK , BLUE , RED , WHITE
2
2
from Ledart import translate
3
- from colorsys import hsv_to_rgb
4
- import operator
5
- import random
6
- import math
7
-
8
- class Vector (object ):
9
- def __init__ (self , ** axes ):
10
- super (Vector , self ).__setattr__ ('axes' , axes )
11
-
12
- def magnitude (self ):
13
- """ for every N square it, sum list. take square root. """
14
- magnitude = sum ([(self .axes [axis ] * self .axes [axis ]) for axis in self .axes ]) ** 0.5
15
- return magnitude
16
-
17
- def set_mag (self , mag ):
18
- self = self .normalize ()
19
- self *= mag
20
-
21
- def dist (self , other ):
22
- deltas = [(self .axes [axis ] - other .axes [axis ]) ** 2 for axis in self .axes ]
23
- return abs (sum (deltas )) ** 0.5
24
-
25
- def normalize (self ):
26
- return self .__div__ (self .magnitude ())
27
-
28
- def limit (self , lim ):
29
- pass
30
-
31
- def heading (self ):
32
- pass
33
-
34
- def rotate (self ):
35
- pass
36
-
37
- def lerp (self ):
38
- pass
39
-
40
- def angleBetween (self ):
41
- pass
42
-
43
- def dot (self ):
44
- pass
45
-
46
- def cross (self ):
47
- pass
48
-
49
- def randomVec (self ):
50
- pass
51
-
52
- def __operation__ (self , op , val ):
53
- result = {}
54
- if isinstance (val , (int , float )):
55
- for axis in self .axes :
56
- result [axis ] = op (self .axes [axis ], val )
57
- elif isinstance (val , Vector ):
58
- for axis in self .axes :
59
- result [axis ] = op (self .axes [axis ], val .axes [axis ])
60
- else :
61
- raise TypeError
62
- return Vector (** result )
63
-
64
- def __add__ (self , val ):
65
- return self .__operation__ (operator .add , val )
66
-
67
- def __sub__ (self , val ):
68
- return self .__operation__ (operator .sub , val )
69
-
70
- def __mul__ (self , val ):
71
- return self .__operation__ (operator .mul , val )
72
-
73
- def __div__ (self , val ):
74
- return self .__operation__ (operator .div , val )
75
-
76
- def __len__ (self ):
77
- return self .mag ()
78
-
79
- def __getattr__ (self , attr ):
80
- if attr in self .axes :
81
- return self .axes [attr ]
82
- elif attr == 'mag' :
83
- return self .magnitude ()
84
- else :
85
- raise AttributeError
86
-
87
- def __setattr__ (self , attr , value ):
88
- if attr in self .axes :
89
- self .axes [attr ] = value
90
- elif attr == 'mag' :
91
- self .set_mag (value )
92
- else :
93
- raise KeyError
94
-
95
- def __str__ (self ):
96
- values = {}
97
- values .update (self .axes )
98
- values ['mag' ] = self .mag
99
- return str (values )
100
3
4
+ from colorsys import *
5
+ from Ledart import Vector
6
+ import random
101
7
102
8
class Ball (object ):
103
9
def __init__ (self , g = None , x = 0 , y = 0 , size = 1 ):
@@ -106,11 +12,12 @@ def __init__(self, g=None, x=0, y=0, size=1):
106
12
self .pos = Vector (x = x , y = y )
107
13
self .size = size
108
14
self .g = g
109
- self .mass = self .size
15
+ self .mass = self .size ** 6 / 2
110
16
self .max_speed = 0
111
17
112
18
def update (self ):
113
19
self .speed += self .accel
20
+ self .speed *= 0.998
114
21
self .pos += self .speed
115
22
self .accel *= 0
116
23
@@ -129,7 +36,7 @@ def handle_collision(self):
129
36
self .speed .y *= - 1
130
37
131
38
def draw (self ):
132
- self .g .draw_circle (self .pos .x , self .pos .y , self .size , BLUE )
39
+ self .g .draw_circle (self .pos .x , self .pos .y , self .size , BLACK )
133
40
134
41
135
42
class MetaBalls (Graphics ):
@@ -144,6 +51,9 @@ def __init__(self, **kwargs):
144
51
# ball.speed += random.randint(20, 80) / 100.
145
52
self .balls .append (ball )
146
53
self .max = 0
54
+ self .totalballsize = 0
55
+ for ball in self .balls :
56
+ self .totalballsize += ball .size
147
57
148
58
def generate (self ):
149
59
self .fill (BLACK )
@@ -156,19 +66,22 @@ def generate(self):
156
66
distance = (ball .pos .dist (Vector (x = x , y = y )) + 1 )
157
67
sumc += ball .size / distance
158
68
159
- color = min (sumc * 80 , 0xff ) / 0xff
160
- self [point ] = [int (c * 0xff ) for c in hsv_to_rgb (color , 1 , 1 )]
69
+ # color = min(sumc * 80, 0xff) / 0xff
70
+ # print(sumc)
71
+ colorfunc = hsv_to_rgb
72
+ color = min (sumc * (0x80 - self .totalballsize ), 0xff ) / 0xff
73
+ self [point ] = [int (c * 0xff ) for c in colorfunc (1 - color , 1 , 1 )]
74
+
161
75
162
76
for ball in self .balls :
163
77
ball .update ()
164
- ball .draw ()
78
+ # ball.draw()
165
79
for otherball in self .balls :
166
- if ball != otherball :
80
+ if ball != otherball : #and ball.pos.dist(otherball.pos) < self.width / 4:
167
81
force = ball .pos - otherball .pos
168
82
dist = max (force .mag , ball .size )
169
83
force = force .normalize ()
170
- G = 0.01
84
+ G = 6.67191 * ( 10 ** - 11 )
171
85
strength = (G * ball .mass * otherball .mass ) / (dist ** 2 )
172
86
force *= strength
173
87
ball .accel -= force
174
- # print(ball.speed)
0 commit comments