Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
P
Project Work
Manage
Activity
Members
Labels
Plan
Issues
5
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package Registry
Container Registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Fabian Del Villar
Project Work
Commits
655ea45d
Commit
655ea45d
authored
1 year ago
by
Fabian
Browse files
Options
Downloads
Patches
Plain Diff
Create test2-5.py
parent
7322af3c
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
test2-5.py
+102
-0
102 additions, 0 deletions
test2-5.py
with
102 additions
and
0 deletions
test2-5.py
0 → 100644
+
102
−
0
View file @
655ea45d
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
()
This diff is collapsed.
Click to expand it.
Preview
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment