コード例 #1
0
    def __init__(self, json):
        if JSONParser._parser(json) == 1:
            raise ValueError("Bad json")
        shapes = []
        self.screen = dict()
        self.palette = dict()

        for i in json["Palette"]:
            self.palette[i] = JSONParser.return_color(dict(),
                                                      json["Palette"][i])

        self.screen["width"] = json["Screen"]["width"]
        self.screen["height"] = json["Screen"]["height"]
        self.screen["bg_color"] = JSONParser.return_color(
            self.palette, json["Screen"]["bg_color"])
        self.screen["fg_color"] = JSONParser.return_color(
            self.palette, json["Screen"]["fg_color"])

        for i in json["Figures"]:
            type = i["type"]
            if type == "point":
                if "color" in i:
                    color = JSONParser.return_color(self.palette, i["color"])
                else:
                    color = self.screen["fg_color"]
                shapes.append(Shape.Point(i["x"], i["y"], color))
            elif type == "polygon":
                if "color" in i:
                    color = JSONParser.return_color(self.palette, i["color"])
                else:
                    color = self.screen["fg_color"]
                shapes.append(Shape.Polygon(i["points"], color))
            elif type == "rectangle":
                if "color" in i:
                    color = JSONParser.return_color(self.palette, i["color"])
                else:
                    color = self.screen["fg_color"]
                shapes.append(
                    Shape.Rectangle(i["x"], i["y"], i["width"], i["height"],
                                    color))
            elif type == "square":
                if "color" in i:
                    color = JSONParser.return_color(self.palette, i["color"])
                else:
                    color = self.screen["fg_color"]
                shapes.append(Shape.Square(i["x"], i["y"], i["size"], color))
            elif type == "circle":
                if "color" in i:
                    color = JSONParser.return_color(self.palette, i["color"])
                else:
                    color = self.screen["fg_color"]
                shapes.append(
                    Shape.Circle(i["x"], i["y"], i["radius"], color=color))
            else:
                print("Bad type of figure")
                raise ValueError("Bad type of figure")

        self.shapes = shapes
コード例 #2
0
import Shape 

rect = Shape.Rectangle(5,10)
threeD = Shape.LiveRectangle(3,7,2)
cube = Shape.Cube(3)
print(rect.area())
print(threeD.volume())
print(cube.volume()) )
コード例 #3
0
import Shape

circle = Shape.Circle(0, 1, "Blue", 9)
rectangle = Shape.Rectangle(1, 0, "White", 14, 9)
triangle = Shape.Triangle(0, 0, "Black", 5, 6, 7)

print("Circle\n Area: {} \n Perimeter: {}".format(circle.getArea(),
                                                  circle.getPerimeter()))
print("\nRectangle\n Area: {} \n Perimeter: {}".format(
    rectangle.getArea(), rectangle.getPerimeter()))
print("\nTriangle\n Area: {} \n Perimeter: {}".format(triangle.getArea(),
                                                      triangle.getPerimeter()))