Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • Fabian/project-work
1 result
Show changes
Commits on Source (11)
import matplotlib.pyplot as plt
import matplotlib.patches as patches
def create_rectangle():
# Prompt for the coordinates of two points
x1 = float(input("Enter the x-coordinate of the first point: "))
y1 = float(input("Enter the y-coordinate of the first point: "))
x2 = float(input("Enter the x-coordinate of the second point: "))
y2 = float(input("Enter the y-coordinate of the second point: "))
# Calculate the rectangle's coordinates
x_min = min(x1, x2)
y_min = min(y1, y2)
width = abs(x2 - x1)
height = abs(y2 - y1)
# Create a figure and axis
fig, ax = plt.subplots()
# Create a rectangle patch
rectangle = patches.Rectangle((x_min, y_min), width, height, linewidth=1, edgecolor='r', facecolor='none')
# Add the rectangle to the plot
ax.add_patch(rectangle)
# Set the x and y axis limits
ax.set_xlim(min(x1, x2) - 1, max(x1, x2) + 1)
ax.set_ylim(min(y1, y2) - 1, max(y1, y2) + 1)
# Set labels and title
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_title('Rectangle')
# Display the plot
plt.show()
# Call the function to create the rectangle
create_rectangle()
\ No newline at end of file
import math
def create_shape():
svg_code = ""
while True:
# Prompt for the shape
shape = input("Enter the shape (rectangle, circle, triangle): ")
if shape == "rectangle":
svg_code += create_rectangle()
elif shape == "circle":
svg_code += create_circle()
elif shape == "triangle":
svg_code += create_triangle()
else:
print("Invalid shape entered.")
# Ask if the user wants to add another shape
add_another = input("Do you want to add another shape? (yes/no): ")
if add_another.lower() != "yes":
break
# Add spacing between shapes
svg_code += f'<rect x="10" y="0" width="0" height="0" />'
# Write the SVG code to a file
with open('shapes.svg', 'w') as file:
file.write(f'<svg xmlns="http://www.w3.org/2000/svg">{svg_code}</svg>')
print("SVG file 'shapes.svg' has been generated.")
def create_rectangle():
# Prompt for the length and width of the rectangle
width = float(input("Enter the width of the rectangle: "))
height = float(input("Enter the height of the rectangle: "))
# Create the SVG code for the rectangle
svg_code = f'<rect width="{width}" height="{height}" fill="none" stroke="red" stroke-width="0.05" />'
return svg_code
def create_circle():
# Prompt for the radius of the circle
radius = float(input("Enter the radius of the circle: "))
# Calculate the diameter
diameter = 2 * radius
# Create the SVG code for the circle
svg_code = f'<circle cx="{radius}" cy="{radius}" r="{radius}" fill="none" stroke="red" stroke-width="0.05" />'
return svg_code
def create_triangle():
# Prompt for the lengths of the triangle's sides
side1 = float(input("Enter the length of the first side of the triangle: "))
side2 = float(input("Enter the length of the second side of the triangle: "))
while True:
side3 = float(input("Enter the length of the third side of the triangle: "))
if side1 + side2 > side3 and side2 + side3 > side1 and side1 + side3 > side2:
break
else:
print("The given side lengths do not satisfy the triangle inequality theorem.")
if side1 + side2 <= side3:
print("The third side length must be smaller.")
elif side2 + side3 <= side1:
print("The first side length must be smaller.")
elif side1 + side3 <= side2:
print("The second side length must be smaller.")
# Create the SVG code for the triangle
svg_code = f'<polygon points="0,0 {side1},0 {side2},{side3}" fill="none" stroke="red" stroke-width="0.05" />'
return svg_code
create_shape()
import math
def create_shape():
svg_code = ""
total_width = 0
while True:
# Prompt for the shape
shape = input("Enter the shape (rectangle, circle, triangle): ")
if shape == "rectangle":
shape_code, shape_width = create_rectangle()
elif shape == "circle":
shape_code, shape_width = create_circle()
elif shape == "triangle":
shape_code, shape_width = create_triangle()
else:
print("Invalid shape entered.")
continue
# Add spacing between shapes
svg_code += f'<rect x="{total_width + 50}" y="0" width="0" height="0" />'
# Update the total width
total_width += shape_width + 50
# Append the shape code to the SVG code
svg_code += shape_code
# Ask if the user wants to add another shape
add_another = input("Do you want to add another shape? (yes/no): ")
if add_another.lower() != "yes":
break
# Write the SVG code to a file
with open('shapes.svg', 'w') as file:
file.write(f'<svg xmlns="http://www.w3.org/2000/svg">{svg_code}</svg>')
print("SVG file 'shapes.svg' has been generated.")
def create_rectangle():
# Prompt for the length and width of the rectangle
width = float(input("Enter the width of the rectangle: "))
height = float(input("Enter the height of the rectangle: "))
# Create the SVG code for the rectangle
svg_code = f'<rect x="{0}" y="{0}" width="{width}" height="{height}" fill="none" stroke="red" stroke-width="0.05" />'
return svg_code, width
def create_circle():
# Prompt for the radius of the circle
radius = float(input("Enter the radius of the circle: "))
# Calculate the diameter
diameter = 2 * radius
# Create the SVG code for the circle
svg_code = f'<circle cx="{radius}" cy="{radius}" r="{radius}" fill="none" stroke="red" stroke-width="0.05" />'
return svg_code, diameter
def create_triangle():
# Prompt for the lengths of the triangle's sides
side1 = float(input("Enter the length of the first side of the triangle: "))
side2 = float(input("Enter the length of the second side of the triangle: "))
while True:
side3 = float(input("Enter the length of the third side of the triangle: "))
if side1 + side2 > side3 and side2 + side3 > side1 and side1 + side3 > side2:
break
else:
print("The given side lengths do not satisfy the triangle inequality theorem.")
if side1 + side2 <= side3:
print("The third side length must be smaller.")
elif side2 + side3 <= side1:
print("The first side length must be smaller.")
elif side1 + side3 <= side2:
print("The second side length must be smaller.")
# Create the SVG code for the triangle
svg_code = f'<polygon points="0,0 {side1},0 {side2},{side3}" fill="none" stroke="red" stroke-width="0.05" />'
# Calculate the width of the triangle
triangle_width = max(side1, side2, side3)
return svg_code, triangle_width
create_shape()
def create_rectangle():
# Prompt for the coordinates of two points
x1 = float(input("Enter the x-coordinate of the first point: "))
y1 = float(input("Enter the y-coordinate of the first point: "))
x2 = float(input("Enter the x-coordinate of the second point: "))
y2 = float(input("Enter the y-coordinate of the second point: "))
# Calculate the rectangle's coordinates
x_min = min(x1, x2)
y_min = min(y1, y2)
width = abs(x2 - x1)
height = abs(y2 - y1)
# Create the SVG code for the rectangle
svg_code = f'<svg xmlns="http://www.w3.org/2000/svg" width="{width}" height="{height}">'
svg_code += f'<rect x="{x_min}" y="{y_min}" width="{width}" height="{height}" fill="none" stroke="red" stroke-width="0.05" />'
svg_code += '</svg>'
# Write the SVG code to a file
with open('rectangle.svg', 'w') as file:
file.write(svg_code)
print("Rectangle SVG file 'rectangle.svg' has been generated.")
# Call the function to create the rectangle
create_rectangle()
\ No newline at end of file
def create_rectangle():
# Prompt for the length and width of the rectangle
width = float(input("Enter the width of the rectangle: "))
height = float(input("Enter the height of the rectangle: "))
# Create the SVG code for the rectangle
svg_code = f'<svg xmlns="http://www.w3.org/2000/svg" width="{width}" height="{height}">'
svg_code += f'<rect x="0" y="0" width="{width}" height="{height}" fill="none" stroke="red" stroke-width="0.05" />'
svg_code += '</svg>'
# Write the SVG code to a file
with open('rectangle.svg', 'w') as file:
file.write(svg_code)
print("Rectangle SVG file 'rectangle.svg' has been generated.")
# Call the function to create the rectangle
create_rectangle()
import math
def create_shape():
# Prompt for the shape
shape = input("Enter the shape (rectangle, circle, triangle, pentagon, hexagon): ")
if shape == "rectangle":
create_rectangle()
elif shape == "circle":
create_circle()
elif shape == "triangle":
create_triangle()
elif shape == "pentagon":
create_polygon(5)
elif shape == "hexagon":
create_polygon(6)
else:
print("Invalid shape entered.")
def create_rectangle():
# Prompt for the length and width of the rectangle
width = float(input("Enter the width of the rectangle: "))
height = float(input("Enter the height of the rectangle: "))
# Create the SVG code for the rectangle
svg_code = f'<svg xmlns="http://www.w3.org/2000/svg" width="{width}" height="{height}">'
svg_code += f'<rect x="0" y="0" width="{width}" height="{height}" fill="none" stroke="red" stroke-width="0.05" />'
svg_code += '</svg>'
# Write the SVG code to a file
with open('rectangle.svg', 'w') as file:
file.write(svg_code)
print("Rectangle SVG file 'rectangle.svg' has been generated.")
def create_circle():
# Prompt for the radius of the circle
radius = float(input("Enter the radius of the circle: "))
# Calculate the diameter
diameter = 2 * radius
# Create the SVG code for the circle
svg_code = f'<svg xmlns="http://www.w3.org/2000/svg" width="{diameter}" height="{diameter}">'
svg_code += f'<circle cx="{radius}" cy="{radius}" r="{radius}" fill="none" stroke="red" stroke-width="0.05" />'
svg_code += '</svg>'
# Write the SVG code to a file
with open('circle.svg', 'w') as file:
file.write(svg_code)
print("Circle SVG file 'circle.svg' has been generated.")
def create_triangle():
# Prompt for the lengths of the triangle's sides
side1 = float(input("Enter the length of the first side of the triangle: "))
side2 = float(input("Enter the length of the second side of the triangle: "))
side3 = float(input("Enter the length of the third side of the triangle: "))
# Create the SVG code for the triangle
svg_code = f'<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100">'
svg_code += f'<polygon points="0,0 {side1},0 {side2},{side3}" fill="none" stroke="red" stroke-width="0.05" />'
svg_code += '</svg>'
# Write the SVG code to a file
with open('triangle.svg', 'w') as file:
file.write(svg_code)
print("Triangle SVG file 'triangle.svg' has been generated.")
def create_polygon(num_sides):
# Prompt for the lengths of the polygon's sides
side_lengths = []
for i in range(num_sides):
side_length = float(input(f"Enter the length of side {i+1} of the polygon: "))
side_lengths.append(side_length)
# Calculate the coordinates of the polygon's vertices
center_x = 100
center_y = 100
radius = 50
angle = 2 * math.pi / num_sides
polygon_points = []
for i in range(num_sides):
x = center_x + radius * math.cos(i * angle)
y = center_y + radius * math.sin(i * angle)
polygon_points.append(f'{x},{y}')
# Create the SVG code for the polygon
svg_code = f'<svg xmlns="http://www.w3.org/2000/svg" width="200" height="200">'
svg_code += f'<polygon points="{polygon_points[0]}'
for point in polygon_points[1:]:
svg_code += f' {point}'
svg_code += f'" fill="none" stroke="red" stroke-width="0.05" />'
svg_code += '</svg>'
# Write the SVG code to a file
with open(f'{num_sides}_sided_polygon.svg', 'w') as file:
file.write(svg_code)
print(f"{num_sides}-sided Polygon SVG file '{num_sides}_sided_polygon.svg' has been generated.")
# Call the function to create the shape
create_shape()
import math
def create_shape():
# Prompt for the number of sides
num_sides = int(input("Enter the number of sides of the shape (0 for circle): "))
if num_sides == 0:
create_circle()
elif num_sides == 3:
create_triangle()
elif num_sides == 4:
create_rectangle()
elif num_sides >= 5:
create_polygon(num_sides)
else:
print("Invalid number of sides entered.")
def create_rectangle():
# Prompt for the length and width of the rectangle
width = float(input("Enter the width of the rectangle: "))
height = float(input("Enter the height of the rectangle: "))
# Create the SVG code for the rectangle
svg_code = f'<svg xmlns="http://www.w3.org/2000/svg" width="{width}" height="{height}">'
svg_code += f'<rect x="0" y="0" width="{width}" height="{height}" fill="none" stroke="red" stroke-width="0.05" />'
svg_code += '</svg>'
# Write the SVG code to a file
with open('rectangle.svg', 'w') as file:
file.write(svg_code)
print("Rectangle SVG file 'rectangle.svg' has been generated.")
def create_circle():
# Prompt for the radius of the circle
radius = float(input("Enter the radius of the circle: "))
# Calculate the diameter
diameter = 2 * radius
# Create the SVG code for the circle
svg_code = f'<svg xmlns="http://www.w3.org/2000/svg" width="{diameter}" height="{diameter}">'
svg_code += f'<circle cx="{radius}" cy="{radius}" r="{radius}" fill="none" stroke="red" stroke-width="0.05" />'
svg_code += '</svg>'
# Write the SVG code to a file
with open('circle.svg', 'w') as file:
file.write(svg_code)
print("Circle SVG file 'circle.svg' has been generated.")
def create_triangle():
# Prompt for the lengths of the triangle's sides
side1 = float(input("Enter the length of the first side of the triangle: "))
side2 = float(input("Enter the length of the second side of the triangle: "))
side3 = float(input("Enter the length of the third side of the triangle: "))
# Create the SVG code for the triangle
svg_code = f'<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100">'
svg_code += f'<polygon points="0,0 {side1},0 {side2},{side3}" fill="none" stroke="red" stroke-width="0.05" />'
svg_code += '</svg>'
# Write the SVG code to a file
with open('triangle.svg', 'w') as file:
file.write(svg_code)
print("Triangle SVG file 'triangle.svg' has been generated.")
def create_polygon(num_sides):
# Prompt for the lengths of the polygon's sides
side_lengths = []
for i in range(num_sides):
side_length = float(input(f"Enter the length of side {i+1} of the polygon: "))
side_lengths.append(side_length)
# Calculate the coordinates of the polygon's vertices
center_x = 100
center_y = 100
radius = 50
angle = 2 * math.pi / num_sides
polygon_points = []
for i in range(num_sides):
x = center_x + radius * math.cos(i * angle)
y = center_y + radius * math.sin(i * angle)
polygon_points.append(f'{x},{y}')
# Create the SVG code for the polygon
svg_code = f'<svg xmlns="http://www.w3.org/2000/svg" width="200" height="200">'
svg_code += f'<polygon points="{polygon_points[0]}'
for point in polygon_points[1:]:
svg_code += f' {point}'
svg_code += f'" fill="none" stroke="red" stroke-width="0.05" />'
svg_code += '</svg>'
# Write the SVG code to a file
with open(f'{num_sides}_sided_polygon.svg', 'w') as file:
file.write(svg_code)
print(f"{num_sides}-sided Polygon SVG file '{num_sides}_sided_polygon.svg' has been generated.")
# Call the function to create the shape
create_shape()
import math
def create_shape():
# Prompt for the shape
shape = input("Enter the shape (rectangle, circle, triangle): ")
if shape == "rectangle":
create_rectangle()
elif shape == "circle":
create_circle()
elif shape == "triangle":
create_triangle()
else:
print("Invalid shape entered.")
def create_rectangle():
# Prompt for the length and width of the rectangle
width = float(input("Enter the width of the rectangle: "))
height = float(input("Enter the height of the rectangle: "))
# Create the SVG code for the rectangle
svg_code = f'<svg xmlns="http://www.w3.org/2000/svg" width="{width}" height="{height}">'
svg_code += f'<rect x="0" y="0" width="{width}" height="{height}" fill="none" stroke="red" stroke-width="0.05" />'
svg_code += '</svg>'
# Write the SVG code to a file
with open('rectangle.svg', 'w') as file:
file.write(svg_code)
print("Rectangle SVG file 'rectangle.svg' has been generated.")
def create_circle():
# Prompt for the radius of the circle
radius = float(input("Enter the radius of the circle: "))
# Calculate the diameter
diameter = 2 * radius
# Create the SVG code for the circle
svg_code = f'<svg xmlns="http://www.w3.org/2000/svg" width="{diameter}" height="{diameter}">'
svg_code += f'<circle cx="{radius}" cy="{radius}" r="{radius}" fill="none" stroke="red" stroke-width="0.05" />'
svg_code += '</svg>'
# Write the SVG code to a file
with open('circle.svg', 'w') as file:
file.write(svg_code)
print("Circle SVG file 'circle.svg' has been generated.")
def create_triangle():
# Prompt for the lengths of the triangle's sides
side1 = float(input("Enter the length of the first side of the triangle: "))
side2 = float(input("Enter the length of the second side of the triangle: "))
while True:
side3 = float(input("Enter the length of the third side of the triangle: "))
if side1 + side2 > side3 and side2 + side3 > side1 and side1 + side3 > side2:
break
else:
print("The given side lengths do not satisfy the triangle inequality theorem.")
if side1 + side2 <= side3:
print("The third side length must be smaller.")
elif side2 + side3 <= side1:
print("The first side length must be smaller.")
elif side1 + side3 <= side2:
print("The second side length must be smaller.")
# Create the SVG code for the triangle
svg_code = f'<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100">'
svg_code += f'<polygon points="0,0 {side1},0 {side2},{side3}" fill="none" stroke="red" stroke-width="0.05" />'
svg_code += '</svg>'
# Write the SVG code to a file
with open('triangle.svg', 'w') as file:
file.write(svg_code)
print("Triangle SVG file 'triangle.svg' has been generated.")
create_shape()
\ No newline at end of file
def create_rectangle():
# Prompt for the coordinates of two points
x1 = float(input("Enter the x-coordinate of the first point: "))
y1 = float(input("Enter the y-coordinate of the first point: "))
x2 = float(input("Enter the x-coordinate of the second point: "))
y2 = float(input("Enter the y-coordinate of the second point: "))
# Calculate the rectangle's coordinates
x_min = min(x1, x2)
y_min = min(y1, y2)
width = abs(x2 - x1)
height = abs(y2 - y1)
# Create the SVG code for the rectangle
svg_code = f'<svg xmlns="http://www.w3.org/2000/svg" width="{width}" height="{height}">'
svg_code += f'<rect x="{x_min}" y="{y_min}" width="{width}" height="{height}" fill="none" stroke="red" />'
svg_code += '</svg>'
# Write the SVG code to a file
with open('rectangle.svg', 'w') as file:
file.write(svg_code)
print("Rectangle SVG file 'rectangle.svg' has been generated.")
# Call the function to create the rectangle
create_rectangle()
print ("hi")
\ No newline at end of file
def create_rectangular_prism():
# Prompt for the dimensions of the rectangular prism
length = float(input("Enter the length of the prism: "))
width = float(input("Enter the width of the prism: "))
height = float(input("Enter the height of the prism: "))
# Create the STL code for the rectangular prism
stl_code = f'solid prism\n'
# Front face
stl_code += f'facet normal 0 0 -1\n'
stl_code += f' outer loop\n'
stl_code += f' vertex 0 0 0\n'
stl_code += f' vertex {length} 0 0\n'
stl_code += f' vertex {length} {width} 0\n'
stl_code += f' endloop\n'
stl_code += f'endfacet\n'
# Back face
stl_code += f'facet normal 0 0 1\n'
stl_code += f' outer loop\n'
stl_code += f' vertex 0 {width} {height}\n'
stl_code += f' vertex {length} {width} {height}\n'
stl_code += f' vertex {length} 0 {height}\n'
stl_code += f' endloop\n'
stl_code += f'endfacet\n'
# Right face
stl_code += f'facet normal 1 0 0\n'
stl_code += f' outer loop\n'
stl_code += f' vertex {length} 0 0\n'
stl_code += f' vertex {length} {width} 0\n'
stl_code += f' vertex {length} {width} {height}\n'
stl_code += f' endloop\n'
stl_code += f'endfacet\n'
# Left face
stl_code += f'facet normal -1 0 0\n'
stl_code += f' outer loop\n'
stl_code += f' vertex 0 {width} {height}\n'
stl_code += f' vertex 0 0 {height}\n'
stl_code += f' vertex 0 0 0\n'
stl_code += f' endloop\n'
stl_code += f'endfacet\n'
# Top face
stl_code += f'facet normal 0 1 0\n'
stl_code += f' outer loop\n'
stl_code += f' vertex 0 {width} {height}\n'
stl_code += f' vertex {length} {width} {height}\n'
stl_code += f' vertex {length} {width} 0\n'
stl_code += f' endloop\n'
stl_code += f'endfacet\n'
# Bottom face
stl_code += f'facet normal 0 -1 0\n'
stl_code += f' outer loop\n'
stl_code += f' vertex 0 0 0\n'
stl_code += f' vertex {length} 0 0\n'
stl_code += f' vertex {length} 0 {height}\n'
stl_code += f' endloop\n'
stl_code += f'endfacet\n'
stl_code += f'endsolid prism\n'
# Write the STL code to a file
with open('prism.stl', 'w') as file:
file.write(stl_code)
print("Rectangular prism STL file 'prism.stl' has been generated.")
# Call the function to create the rectangular prism
create_rectangular_prism()
\ No newline at end of file
import pythreejs as three
from IPython.display import display
def create_rectangular_prism():
# Prompt for the dimensions of the rectangular prism
length = float(input("Enter the length of the prism: "))
width = float(input("Enter the width of the prism: "))
height = float(input("Enter the height of the prism: "))
# Create the STL code for the rectangular prism
stl_code = f'solid prism\n'
# Front face
stl_code += f'facet normal 0 0 -1\n'
stl_code += f' outer loop\n'
stl_code += f' vertex 0 0 0\n'
stl_code += f' vertex {length} 0 0\n'
stl_code += f' vertex {length} {width} 0\n'
stl_code += f' endloop\n'
stl_code += f'endfacet\n'
# Back face
stl_code += f'facet normal 0 0 1\n'
stl_code += f' outer loop\n'
stl_code += f' vertex 0 {width} {height}\n'
stl_code += f' vertex {length} {width} {height}\n'
stl_code += f' vertex {length} 0 {height}\n'
stl_code += f' endloop\n'
stl_code += f'endfacet\n'
# Right face
stl_code += f'facet normal 1 0 0\n'
stl_code += f' outer loop\n'
stl_code += f' vertex {length} 0 0\n'
stl_code += f' vertex {length} {width} 0\n'
stl_code += f' vertex {length} {width} {height}\n'
stl_code += f' endloop\n'
stl_code += f'endfacet\n'
# Left face
stl_code += f'facet normal -1 0 0\n'
stl_code += f' outer loop\n'
stl_code += f' vertex 0 {width} {height}\n'
stl_code += f' vertex 0 0 {height}\n'
stl_code += f' vertex 0 0 0\n'
stl_code += f' endloop\n'
stl_code += f'endfacet\n'
# Top face
stl_code += f'facet normal 0 1 0\n'
stl_code += f' outer loop\n'
stl_code += f' vertex 0 {width} {height}\n'
stl_code += f' vertex {length} {width} {height}\n'
stl_code += f' vertex {length} {width} 0\n'
stl_code += f' endloop\n'
stl_code += f'endfacet\n'
# Bottom face
stl_code += f'facet normal 0 -1 0\n'
stl_code += f' outer loop\n'
stl_code += f' vertex 0 0 0\n'
stl_code += f' vertex {length} 0 0\n'
stl_code += f' vertex {length} 0 {height}\n'
stl_code += f' endloop\n'
stl_code += f'endfacet\n'
stl_code += f'endsolid prism\n'
# Write the STL code to a file
with open('prism.stl', 'w') as file:
file.write(stl_code)
# Create the rectangular prism geometry
geometry = three.BoxGeometry(length=length, width=width, height=height)
# Create the material and mesh
material = three.MeshBasicMaterial(color='lightblue', side='DoubleSide')
mesh = three.Mesh(geometry=geometry, material=material)
# Create the scene and add the mesh
scene = three.Scene()
scene.add(mesh)
# Create the camera and renderer
camera = three.PerspectiveCamera(position=[10, 10, 10], aspect=1)
renderer = three.Renderer(camera=camera, scene=scene, controls=[three.OrbitControls(controlling=camera)])
# Render the scene
display(renderer)
# Call the function to create the rectangular prism and generate the image
create_rectangular_prism()