cylinder drawing
This commit is contained in:
parent
f30e866ee4
commit
d1cb78a742
59
cylinder_drawing_example.py
Normal file
59
cylinder_drawing_example.py
Normal file
@ -0,0 +1,59 @@
|
||||
import cairocffi as cairo
|
||||
import math
|
||||
|
||||
|
||||
def draw_cylinders(width, height, filename):
|
||||
# Setup surface and context
|
||||
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, width, height)
|
||||
ctx = cairo.Context(surface)
|
||||
|
||||
# White background
|
||||
ctx.set_source_rgb(1, 1, 1)
|
||||
ctx.paint()
|
||||
|
||||
# Cylinder dimensions
|
||||
cyl_width = 80
|
||||
cyl_height = 200
|
||||
top_ellipse_height = 30
|
||||
spacing = 50
|
||||
|
||||
# Starting position
|
||||
start_x = 50
|
||||
y = 50
|
||||
|
||||
for i in range(3):
|
||||
x = start_x + i * (cyl_width + spacing)
|
||||
|
||||
# 1. Draw Side Gradient (Rect + Arc)
|
||||
# Create a vertical linear gradient: White (top) to Blue (bottom)
|
||||
pat = cairo.LinearGradient(x, y, x+cyl_width, y)
|
||||
pat.add_color_stop_rgb(0, 1, 1, 1) # White top
|
||||
pat.add_color_stop_rgb(1, 0.2, 0.2, 1) # Blue bottom
|
||||
|
||||
# Draw the rectangle body
|
||||
ctx.rectangle(x, y + top_ellipse_height / 2, cyl_width, cyl_height - top_ellipse_height / 2)
|
||||
ctx.set_source(pat)
|
||||
ctx.fill()
|
||||
|
||||
# 2. Draw Top Ellipse (White)
|
||||
ctx.save()
|
||||
ctx.translate(x + cyl_width / 2, y + top_ellipse_height / 2)
|
||||
ctx.scale(1, top_ellipse_height / (cyl_width / 2) * 0.5) # Ellipse scale
|
||||
ctx.arc(0, 0, cyl_width / 2, 0, 2 * math.pi)
|
||||
ctx.restore()
|
||||
|
||||
ctx.set_source_rgb(1, 1, 1) # White
|
||||
ctx.fill_preserve()
|
||||
|
||||
# Outline for better visibility
|
||||
ctx.set_source_rgb(0, 0, 0)
|
||||
ctx.set_line_width(1)
|
||||
ctx.stroke()
|
||||
|
||||
# Save to PNG
|
||||
surface.write_to_png(filename)
|
||||
print(f"Saved {filename}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
draw_cylinders(450, 300, "cylinders.png")
|
||||
@ -10,6 +10,7 @@ import ee
|
||||
import numpy as np
|
||||
import pandas as pd
|
||||
import cairocffi as cairo
|
||||
import math
|
||||
|
||||
class Topology:
|
||||
def __init__(self,
|
||||
@ -161,26 +162,22 @@ class Topology:
|
||||
values[r, c] = np.mean(this_array[this_array != 0])
|
||||
return values
|
||||
|
||||
def create_rectangle_svg_file(self, rotation=None,file_index=None):
|
||||
def create_cylinder_svg_file(self, rotation=None,file_index=None):
|
||||
|
||||
# subfunction to draw an array, options are line y/n, fill y/n, line and fill color
|
||||
def svg_draw_list(svg_context, draw_list, line_color="White",fill_shape=False):
|
||||
if line_color == "White":
|
||||
def svg_draw_cylinder(svg_context: cairo.Context, x1,y1,x2,y2,gradient=None):
|
||||
circlew = self.linemap_xscale / 3
|
||||
halfcirclew = circlew / 2
|
||||
svg_context.set_source_rgb(1, 1, 1)
|
||||
if line_color == "Black":
|
||||
svg_context.set_source_rgb(0, 0, 0)
|
||||
|
||||
for item in range(len(draw_list)):
|
||||
if item == 0:
|
||||
svg_context.move_to(draw_list[item][0],draw_list[item][1])
|
||||
else:
|
||||
svg_context.line_to(draw_list[item][0],draw_list[item][1])
|
||||
if fill_shape:
|
||||
svg_context.close_path()
|
||||
svg_context.fill_preserve()
|
||||
svg_context.arc(x1,y1,circlew,0,2*math.pi)
|
||||
svg_context.stroke()
|
||||
|
||||
svg_context.arc(x2,y2,circlew, 0,2*math.pi)
|
||||
svg_context.stroke()
|
||||
|
||||
svg_context.move_to(x1-1,y1-1)
|
||||
svg_context.rectangle(x1-1,y1-1,x1+1,y2+1)
|
||||
svg_context.stroke()
|
||||
# if no refetch, check if file exists
|
||||
if os.path.exists(self.elevation_filename):
|
||||
print("Elevation file exists")
|
||||
@ -247,9 +244,9 @@ class Topology:
|
||||
z = norm_data[r,c]
|
||||
x,y = project(c, r, z) # now need to make array of x,y vals and pass to drawing func
|
||||
draw_list.append((x,y))
|
||||
svg_draw_cylinder(cr,x,y,x,y-z)
|
||||
print("***" + str(draw_list))
|
||||
svg_draw_list(cr, draw_list, line_color="Black", fill_shape=True)
|
||||
svg_draw_list(cr, draw_list, line_color="White", fill_shape=False)
|
||||
#svg_draw_list(cr, draw_list, line_color="Black", fill_shape=True)
|
||||
|
||||
|
||||
surface.write_to_png(svg_filename.replace(".svg", ".topo.png"))
|
||||
@ -371,15 +368,16 @@ def main():
|
||||
topology = Topology(country="Switzerland",
|
||||
storage_directory="/Users/he/PycharmProjects/toposhirt/outputfiles",
|
||||
subregion_mode=False,
|
||||
linemap_cols=200,
|
||||
linemap_rows=200,
|
||||
linemap_cols=50,
|
||||
linemap_rows=50,
|
||||
linemap_size=(1080,1080),
|
||||
linemap_width=0.3)
|
||||
topology.list_subunits()
|
||||
print(topology.get_boundingbox())
|
||||
topology.create_svg_file()
|
||||
for r in range(4*360):
|
||||
print("Rotating by [{}] degrees".format(r/4.0))
|
||||
#topology.create_svg_file()
|
||||
topology.create_cylinder_svg_file()
|
||||
#for r in range(4*360):
|
||||
#print("Rotating by [{}] degrees".format(r/4.0))
|
||||
#topology.create_svg_file(rotation=(r / 4.0), file_index=r)
|
||||
|
||||
sys.exit(0)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user