Jelajahi Sumber

optimize scripts

johann 5 tahun lalu
induk
melakukan
c0938dd9eb
3 mengubah file dengan 55 tambahan dan 20 penghapusan
  1. 14 10
      scripts/3d_room.py
  2. 25 7
      scripts/fractal_tree.py
  3. 16 3
      scripts/fractal_tree_random.py

+ 14 - 10
scripts/3d_room.py

@@ -3,25 +3,29 @@ from lazpaint import tools, image, layer, dialog
 def line(x, y, x2, y2):
     tools.choose(tools.PEN)
     tools.mouse([(x, y), (x2, y2)])
-    tools.keys(tools.KEY_RETURN)
-
 
 ZOOM = dialog.input_value("Zoom (Between 0.1 and 0.4 it creates a 3d room, more than 0.5 to 0.9 it creates a cross with a rectangle)", 0.25)
 
+image.do_begin()
 layer.new()
+w = image.get_width()
+h = image.get_height() 
+
 #top left
-line(0, 0, image.get_width() * ZOOM, image.get_height() * ZOOM)
+line(0, 0, w * ZOOM, h * ZOOM)
 #bottom left
-line(0, image.get_height(), image.get_width() * ZOOM, image.get_height() - (image.get_height() * ZOOM))
+line(0, h, w * ZOOM, h - (h * ZOOM))
 #top right
-line(image.get_width(), 0, image.get_width() - (image.get_width() * ZOOM), image.get_height() * ZOOM)
+line(w, 0, w - (w * ZOOM), h * ZOOM)
 #bottom right
-line(image.get_width(), image.get_height(), image.get_width() - (image.get_width() * ZOOM), image.get_height() - (image.get_height() * ZOOM))
+line(w, h, w - (w * ZOOM), h - (h * ZOOM))
 #top
-line(image.get_width() * ZOOM, image.get_height() * ZOOM, image.get_width() - (image.get_width() * ZOOM),  image.get_height() * ZOOM)
+line(w * ZOOM, h * ZOOM, w - (w * ZOOM),  h * ZOOM)
 #bottom
-line(image.get_width() * ZOOM, image.get_height() - (image.get_height() * ZOOM), image.get_width() - (image.get_width() * ZOOM), image.get_height() - (image.get_height() * ZOOM))
+line(w * ZOOM, h - (h * ZOOM), w - (w * ZOOM), h - (h * ZOOM))
 #left
-line(image.get_width() * ZOOM, image.get_height() * ZOOM, image.get_width() * ZOOM, image.get_height() - (image.get_height() * ZOOM))
+line(w * ZOOM, h * ZOOM, w * ZOOM, h - (h * ZOOM))
 #right
-line(image.get_width() - (image.get_width() * ZOOM), image.get_height() - (image.get_height() * ZOOM), image.get_width() - (image.get_width() * ZOOM), image.get_height() * ZOOM)
+line(w - (w * ZOOM), h - (h * ZOOM), w - (w * ZOOM), h * ZOOM)
+
+image.do_end()

+ 25 - 7
scripts/fractal_tree.py

@@ -1,14 +1,33 @@
 from lazpaint import tools, image, layer, dialog
 import math, random
 
+line_buf = []
+
 def line(x, y, x2, y2):
+  global line_buf
+  if len(line_buf) > 0 and line_buf[-1] == (x, y):
+    line_buf.append( (x2, y2) )
+  else:
+    flush_line()
+    line_buf = [(x, y), (x2, y2)]
+
+def flush_line():
+  global line_buf
+  if len(line_buf) > 0:
     tools.choose(tools.PEN)
-    tools.mouse([(x, y), (x2, y2)])
-    tools.keys(tools.KEY_RETURN)
+    tools.mouse(line_buf)
+  line_buf = []
 
-MULTIPLIER = dialog.input_value("Zoom", 10)
-ANGLE = dialog.input_value("Angle", 45)
 DEG_TO_RAD = math.pi / 180
+ANGLE = abs(dialog.input_value("Angle (< 90)", 45))
+if ANGLE >= 90:
+  dialog.show_message("Invalid angle")  
+DEFAULT_SIZE_Y = 7*8/2*2 * (1.14+math.cos(ANGLE * DEG_TO_RAD))/2.14
+MULTIPLIER = image.get_height() / DEFAULT_SIZE_Y
+
+ZOOM = dialog.input_value("Vertical size (%)", 95)
+MULTIPLIER = MULTIPLIER * ZOOM/100
+
 
 def drawTree(x1, y1, angle, depth):
     if (depth > 0):
@@ -17,8 +36,7 @@ def drawTree(x1, y1, angle, depth):
         line(x1, y1, x2, y2)
         drawTree(x2, y2, angle - ANGLE, depth - 2)
         drawTree(x2, y2, angle + ANGLE, depth - 2)
-        drawTree(x2, y2, angle - ANGLE, depth - 2)
-        drawTree(x2, y2, angle + ANGLE, depth - 2)
 
 layer.new()
-drawTree(image.get_width() / 2, image.get_height(), -90, 14)
+drawTree(image.get_width() / 2, image.get_height(), -90, 14)
+flush_line()

+ 16 - 3
scripts/fractal_tree_random.py

@@ -1,10 +1,22 @@
 from lazpaint import tools, image, layer, dialog
 import math, random
 
+line_buf = []
+
 def line(x, y, x2, y2):
+  global line_buf
+  if len(line_buf) > 0 and line_buf[-1] == (x, y):
+    line_buf.append( (x2, y2) )
+  else:
+    flush_line()
+    line_buf = [(x, y), (x2, y2)]
+
+def flush_line():
+  global line_buf
+  if len(line_buf) > 0:
     tools.choose(tools.PEN)
-    tools.mouse([(x, y), (x2, y2)])
-    tools.keys(tools.KEY_RETURN)
+    tools.mouse(line_buf)
+  line_buf = []
 
 MULTIPLIER = dialog.input_value("Zoom", 10)
 DEG_TO_RAD = math.pi / 180
@@ -20,4 +32,5 @@ def drawTree(x1, y1, angle, depth):
         drawTree(x2, y2, angle + random.randint(15,50), depth - 4)
 
 layer.new()
-drawTree(image.get_width() / 2, image.get_height(), -91, 9)
+drawTree(image.get_width() / 2, image.get_height(), -91, 9)
+flush_line()