Data structure modeling shapes. attribute _type is one of "polygon", "image", "compound" attribute _data is - depending on _type a poygon-tuple, an image or a list constructed using the addcomponent method.
| 869 | |
| 870 | |
| 871 | class Shape(object): |
| 872 | """Data structure modeling shapes. |
| 873 | |
| 874 | attribute _type is one of "polygon", "image", "compound" |
| 875 | attribute _data is - depending on _type a poygon-tuple, |
| 876 | an image or a list constructed using the addcomponent method. |
| 877 | """ |
| 878 | def __init__(self, type_, data=None): |
| 879 | self._type = type_ |
| 880 | if type_ == "polygon": |
| 881 | if isinstance(data, list): |
| 882 | data = tuple(data) |
| 883 | elif type_ == "image": |
| 884 | if isinstance(data, str): |
| 885 | if data.lower().endswith(".gif") and isfile(data): |
| 886 | data = TurtleScreen._image(data) |
| 887 | # else data assumed to be Photoimage |
| 888 | elif type_ == "compound": |
| 889 | data = [] |
| 890 | else: |
| 891 | raise TurtleGraphicsError("There is no shape type %s" % type_) |
| 892 | self._data = data |
| 893 | |
| 894 | def addcomponent(self, poly, fill, outline=None): |
| 895 | """Add component to a shape of type compound. |
| 896 | |
| 897 | Arguments: poly is a polygon, i. e. a tuple of number pairs. |
| 898 | fill is the fillcolor of the component, |
| 899 | outline is the outline color of the component. |
| 900 | |
| 901 | call (for a Shapeobject namend s): |
| 902 | -- s.addcomponent(((0,0), (10,10), (-10,10)), "red", "blue") |
| 903 | |
| 904 | Example: |
| 905 | >>> poly = ((0,0),(10,-5),(0,10),(-10,-5)) |
| 906 | >>> s = Shape("compound") |
| 907 | >>> s.addcomponent(poly, "red", "blue") |
| 908 | >>> # .. add more components and then use register_shape() |
| 909 | """ |
| 910 | if self._type != "compound": |
| 911 | raise TurtleGraphicsError("Cannot add component to %s Shape" |
| 912 | % self._type) |
| 913 | if outline is None: |
| 914 | outline = fill |
| 915 | self._data.append([poly, fill, outline]) |
| 916 | |
| 917 | |
| 918 | class Tbuffer(object): |
no outgoing calls
no test coverage detected