villares 2019-04-08 01:01:43 -03:00
rodzic c848b78eb8
commit a4a2eaff98
3 zmienionych plików z 305 dodań i 0 usunięć

Wyświetl plik

@ -0,0 +1,40 @@
"""
Alexandre B A Villares http://abav.lugaralgum.com - GPL v3
A helper for the Processing gifAnimation library https://github.com/extrapixel/gif-animation/tree/3.0
Download from https://github.com/villares/processing-play/blob/master/export_GIF/unzip_and_move_to_libraries_GifAnimation.zip
This helper was inspired by an example by Art Simon https://github.com/APCSPrinciples/AnimatedGIF/
# add at the start of your sketch:
add_library('gifAnimation')
from gif_exporter import gif_export
# add at the end of draw():
gif_export(GifMaker)
"""
def gif_export(GifMaker, # gets a reference to the library
filename="exported", # .gif will be added
repeat=0, # 0 makes it an "endless" animation
quality=255, # quality range 0 - 255
delay=200, # this is quick
frames=0, # 0 will stop on keyPressed or frameCount >= 100000
finish=False): # force stop
global gifExporter
try:
gifExporter
except NameError:
gifExporter = GifMaker(this, filename + ".gif")
gifExporter.setRepeat(repeat)
gifExporter.setQuality(quality)
gifExporter.setDelay(delay)
gifExporter.addFrame()
if frames == 0:
if keyPressed and key == "e":
finish = True
if finish:
gifExporter.finish()
print("gif saved")
exit()

Wyświetl plik

@ -0,0 +1,224 @@
# Alexandre B A Villares - https://abav.lugaralgum.com/sketch-a-day
SKETCH_NAME, OUTPUT = "sketch_190408a", ".gif"
"""
With glue tabs!
"""
# add_library('Gifahnimation')
# from gif_exporter import gif_export
# add_library('peasycam')
from third_point import third_point
CUT_COLOR = color(200, 0, 0)
ENG_COLOR = color(0, 0, 200)
TAB_W = 10
TAB_A = radians(30)
box_d, box_w, box_h = 100, 100, 100
ah = bh = ch = dh = box_h
def setup():
size(850, 500, P3D)
#global cam
# cam = Peasycham(this, 300)
hint(ENABLE_DEPTH_SORT)
smooth(16)
strokeWeight(2)
def draw():
background(200)
# cam.beginHUD()
with pushMatrix():
translate(100, 350)
draw_unfolded()
# cam.endHUD()
with pushMatrix():
translate(width / 2, height / 2) # comment out if with PeasyCam
rotateX(QUARTER_PI)
rotateZ(PI)
translate(-300, -50, -100)
draw_3d()
def draw_unfolded():
noFill()
origin = (0, 0)
bh_2d = (0, -bh)
ch_2d = (box_w, -ch)
c0_2d = (box_w, 0)
dh_2d = (box_w + box_d, -dh)
d0_2d = (box_w + box_d, 0)
ah_2d = (box_w * 2 + box_d, -ah)
a0_2d = (box_w * 2 + box_d, 0)
stroke(ENG_COLOR)
# verticals
line_draw(c0_2d, ch_2d)
line_draw(d0_2d, dh_2d)
line_draw(a0_2d, ah_2d)
# lower triangle
bd = dist(0, 0, bh, box_w, box_d, dh)
cd = dist(box_w, 0, ch, box_w, box_d, dh)
d2_2d = third_point(bh_2d, ch_2d, bd, cd)[0] # gets the first solution
line_draw(bh_2d, ch_2d)
line_draw(bh_2d, d2_2d)
line_draw(ch_2d, d2_2d)
# upper triangle
ab = dist(0, ah, box_w, bh)
ad = dist(0, ah, box_d, dh)
a2_2d = third_point(d2_2d, bh_2d, ab, ad)[1] # gets the second solution
line_draw(bh_2d, a2_2d)
line_draw(d2_2d, a2_2d)
line_draw(origin, bh_2d)
# floor face
rect(0, 0, box_w, box_d)
stroke(CUT_COLOR)
# top tabs
glue_tab(d2_2d, ch_2d, TAB_W, TAB_A)
glue_tab(bh_2d, a2_2d, TAB_W, TAB_A)
glue_tab(a2_2d, d2_2d, TAB_W, TAB_A)
# middle tab
glue_tab(origin, bh_2d, TAB_W, TAB_A)
# floor tabs
glue_tab((0, box_d), origin, TAB_W, TAB_A)
glue_tab((box_w, box_d), (0, box_d), TAB_W, TAB_A)
glue_tab((box_w, 0), (box_w, box_d), TAB_W, TAB_A)
# main outline cut
poly_draw((ch_2d, dh_2d, ah_2d,
(box_w * 2 + box_d * 2, -bh),
(box_w * 2 + box_d * 2, 0),
c0_2d), closed=False)
def draw_3d():
stroke(0)
fill(255, 200)
# floor face
poly_draw(((0, 0, 0),
(box_w, 0, 0),
(box_w, box_d, 0),
(0, box_d, 0)))
# face 0
poly_draw(((0, 0, bh),
(box_w, 0, ch),
(box_w, 0, 0),
(0, 0, 0)))
# face 1
poly_draw(((box_w, box_d, dh),
(box_w, 0, ch),
(box_w, 0, 0),
(box_w, box_d, 0)))
# face 2
poly_draw(((box_w, box_d, dh),
(0, box_d, ah),
(0, box_d, 0),
(box_w, box_d, 0)))
# face 3
poly_draw(((0, 0, bh),
(0, box_d, ah),
(0, box_d, 0),
(0, 0, 0)))
# first triangle
poly_draw(((0, 0, bh),
(box_w, box_d, dh),
(0, box_d, ah)))
# second triangle
poly_draw(((0, 0, bh),
(box_w, box_d, dh),
(box_w, 0, ch)))
# diagonal
stroke(ENG_COLOR)
line(0, 0, bh, box_w, box_d, dh)
def poly_draw(points, closed=True):
beginShape()
for p in points:
vertex(*p)
if closed:
endShape(CLOSE)
else:
endShape()
def line_draw(p1, p2):
line(p1[0], p1[1], p2[0], p2[1])
def glue_tab(p1, p2, w, a=QUARTER_PI):
f2 = atan2(p1[0] - p2[0], p1[1] - p2[1]) - a
f1 = atan2(p1[0] - p2[0], p1[1] - p2[1]) + a + PI
r = w / sin(a) # radius to get the right width
fl = PVector(p1[0] + r * sin(f1),
p1[1] + r * cos(f1))
fr = PVector(p2[0] + r * sin(f2),
p2[1] + r * cos(f2))
d1 = dist(p1[0], p1[1], p2[0], p2[1])
if d1 > 2 * r * cos(a): # 'normal' trapezoidal flap
beginShape()
vertex(*p1) # vertex(p1[0], p1[1])
vertex(*fl) # vertex(fl.x, fl.y)
vertex(*fr) # vertex(fr.x, fr.y)
vertex(*p2) # vertex(p2[0], p2[1])
endShape()
else: # short triangular flap
fm = (fl + fr) / 2
beginShape()
vertex(*p1) # (p1[0], p1[1])
vertex(*fm) # (fm.x, fm.y)
vertex(*p2) # (p2[0], p2[1])
endShape()
def keyPressed():
global ah, bh, ch, dh, box_w, box_d, box_h
# save frame on GIF
#gif_export(GifMaker, filename=SKETCH_NAME)
if key == "q":
ah += 5
if key == "a" and ah > 5:
ah -= 5
if key == "w":
bh += 5
if key == "s" and bh > 5:
bh -= 5
if key == "e":
ch += 5
if key == "d" and ch > 5:
ch -= 5
if key == "r":
dh += 5
if key == "f" and dh > 5:
dh -= 5
if key in ("+", "="):
box_h += 5
ah += 5
bh += 5
ch += 5
dh += 5
if (key == "-" and box_h > 5 and ah > 5 and bh > 5 and ch > 5 and dh > 5):
box_h -= 5
ah -= 5
bh -= 5
ch -= 5
dh -= 5
if keyCode == UP and box_d + box_w < 220:
box_d += 5
if keyCode == DOWN and box_d > 5:
box_d -= 5
if keyCode == RIGHT and box_w + box_d < 220:
box_w += 5
if keyCode == LEFT and box_w > 5:
box_w -= 5
if key == " ":
slowly_reset_values()
def slowly_reset_values():
global box_w, box_d, box_h, ah, bh, ch, dh
box_w += (100 - box_w) / 2
box_d += (100 - box_d) / 2
box_h += (100 - box_h) / 2
ah += (box_h - ah) / 2
bh += (box_h - bh) / 2
ch += (box_h - ch) / 2
dh += (box_h - dh) / 2

Wyświetl plik

@ -0,0 +1,41 @@
"""
Code adapted from code by Monkut https://stackoverflow.com/users/24718/monkut
found at https://stackoverflow.com/questions/4001948/drawing-a-triangle-in-a-coordinate-plane-given-its-three-sides
"""
class NoTrianglePossible(BaseException):
pass
def third_point(point_a, point_b, ac_length, bc_length):
"""
Returns two point_c options given: point_a, point_b, ac_length, bc_length
"""
# To allow use of tuples, creates or recreates PVectors
point_a, point_b = PVector(*point_a), PVector(*point_b)
# check if a triangle is possible
ab_length = point_a.dist(point_b)
if ab_length > (ac_length + bc_length) or ab_length < abs(ac_length - bc_length):
raise NoTrianglePossible("The sides do not form a triangle")
# get the length to the vertex of the right triangle formed,
# by the intersection formed by circles a and b
ad_length = (ab_length ** 2
+ ac_length ** 2
- bc_length ** 2) / (2.0 * ab_length)
# get the height of the line at a right angle from a_length
h = sqrt(abs(ac_length ** 2 - ad_length ** 2))
# Calculate the mid PVector (point_d), needed to calculate point_c(1|2)
d_x = point_a.x + ad_length * (point_b.x - point_a.x) / ab_length
d_y = point_a.y + ad_length * (point_b.y - point_a.y) / ab_length
point_d = PVector(d_x, d_y)
# get point_c locations
c_x1 = point_d.x + h * (point_b.y - point_a.y) / ab_length
c_x2 = point_d.x - h * (point_b.y - point_a.y) / ab_length
c_y1 = point_d.y - h * (point_b.x - point_a.x) / ab_length
c_y2 = point_d.y + h * (point_b.x - point_a.x) / ab_length
return PVector(c_x1, c_y1), PVector(c_x2, c_y2)