-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsaa495_mtf83_cs126-1_lab-9.py
112 lines (88 loc) · 3.25 KB
/
saa495_mtf83_cs126-1_lab-9.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# Scott Ames / Matthew Flanders
# saa495 / mtf83
# Section 01
# Lab 9: Draw Stars
import turtle
def read_coords(file):
# tuple to hold seperate star dictionaries
coordinates_dict = {}
magnitudedict = {}
star_name_dict = {}
# opening the file and reading the lines
inputfile = open(file, "r")
lines = inputfile.readlines()
# organize files into appropriate dictionaries
for line in lines:
splitline = line.split(" ")
xy = (splitline[0], splitline[1])
HDN = splitline[3]
magnitude = splitline[4]
HRN = splitline[5]
# replace \n
if "\n" in HRN:
HRN.replace("\n", "")
if len(splitline) > 6:
starname = splitline[6:]
starname = str(starname)
coordinates_dict[HDN] = (xy)
magnitudedict[HDN] = (magnitude)
return (coordinates_dict, magnitudedict)
def plot_plain_stars(picture_size, coordinates_dict):
# step 0: setup turtle (background color, fill color, etc)
turtle.fillcolor("white")
turtle.bgcolor("black")
turtle.setup(picture_size, picture_size)
# Step 1: Loop through the keys in the dictionary (HDN #'s) and
# get the x, y coords for that star by the HDN and
# Scale them to your picture
for star_HDN in coordinates_dict.keys():
coordinates = coordinates_dict[star_HDN]
# Ex: (x, y)
xCoord = float(coordinates[0] * (picture_size / 2))
yCoord = float(coordinates[1] * (picture_size / 2))
# Step 3: Pen up, go to those coords
turtle.pu()
turtle.goto(xCoord, yCoord)
# Step 4: Pen down, draw a square / fill square
turtle.pd()
for i in range(4):
turtle.forward(2)
turtle.right(90)
turtle.exitonclick()
def plot_by_magnitude(picture_size, coordinates_dict, magnitudedict):
# setup turtle (background color, fill color, etc)
turtle.fillcolor("white")
turtle.bgcolor("black")
turtle.pencolor("white")
turtle.setup(picture_size, picture_size)
turtle.speed(0)
turtle.tracer(8, 25)
# Loop through the keys in the dictionary (HDN #'s) and
# get the x, y coords for that star by the HDN and
# Scale them to your picture
for star_HDN in coordinates_dict.keys():
coordinates = coordinates_dict[star_HDN]
# Ex: (x, y)
xCoord = float(coordinates[0]) * (float(picture_size) / 2)
yCoord = float(coordinates[1]) * (float(picture_size) / 2)
star_magnitude = magnitudedict[star_HDN]
star_magnitude = round(10.0 / (float(star_magnitude) + 2))
# set max magnitude
if star_magnitude > 8:
star_magnitude = 8
# Pen up, go to those coords
turtle.pu()
turtle.goto(xCoord, yCoord)
# Pen down, draw a square / fill square
turtle.pd()
turtle.begin_fill()
for i in range(4):
turtle.forward(star_magnitude)
turtle.right(90)
turtle.end_fill()
turtle.exitonclick()
def main():
stars_tuple = read_coords("stars.txt")
plot_by_magnitude(500, stars_tuple[0], stars_tuple[1])
if __name__ == "__main__":
main()