Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/lengthproblem.py
blob: f43cad38a38977cfcfa78db11111c68f844f66e2 (plain)
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# Copyright 2008 by Peter Moxhay and Wade Brainerd.  
# This file is part of Math.
#
# Math is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 
# Math is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with Math.  If not, see <http://www.gnu.org/licenses/>.
from objectarea import ObjectArea, Color
from vector import Vector

from shapeobject import ShapeObject
from symbolobject import SymbolObject
from instructionsobject import InstructionsObject
from problem import Problem

import gtk, math, random

class LengthProblem(Problem):
    """
    Generates a problem in which two lengths are compared.
    """
    
    def __init__(self, container):
        self.container = container
        
        self.generate_problem()
        self.show_problem()
        
        self.answer = self.find_answer()
        
        self.container.moons_visible = False

    def generate_problem(self):     
        # Choose two random colors.
        (color1, color2) = random.choice([(Color.RED, Color.BLUE), (Color.BLUE, Color.RED), \
            (Color.RED, Color.GREEN), (Color.GREEN, Color.RED), \
            (Color.BLUE, Color.GREEN), (Color.GREEN, Color.BLUE)]) 
        
        # Some rectangles of different length.   
        LENGTH_0 = [ Vector(0, 0), Vector(50, 0), Vector(50, 175), Vector(0, 175) ]   
        LENGTH_1 = [ Vector(0, 0), Vector(50, 0), Vector(50, 200), Vector(0, 200) ]
        LENGTH_2 = [ Vector(0, 0), Vector(50, 0), Vector(50, 225), Vector(0, 225) ]     
        LENGTH_3 = [ Vector(0, 0), Vector(50, 0), Vector(50, 250), Vector(0, 250) ]
        LENGTH_4 = [ Vector(0, 0), Vector(50, 0), Vector(50, 275), Vector(0, 275) ]
        LENGTH_5 = [ Vector(0, 0), Vector(50, 0), Vector(50, 300), Vector(0, 300) ]       
        LENGTH_6 = [ Vector(0, 0), Vector(50, 0), Vector(50, 325), Vector(0, 325) ]
        LENGTH_7 = [ Vector(0, 0), Vector(50, 0), Vector(50, 350), Vector(0, 350) ]
        LENGTH_8 = [ Vector(0, 0), Vector(50, 0), Vector(50, 375), Vector(0, 375) ]
        LENGTH_9 = [ Vector(0, 0), Vector(50, 0), Vector(50, 400), Vector(0, 400) ]
        LENGTH_10 = [ Vector(0, 0), Vector(50, 0), Vector(50, 425), Vector(0, 425) ]
        LENGTH_11 = [ Vector(0, 0), Vector(50, 0), Vector(50, 450), Vector(0, 450) ]
        LENGTH_12 = [ Vector(0, 0), Vector(50, 0), Vector(50, 475), Vector(0, 475) ]
        LENGTH_13 = [ Vector(0, 0), Vector(50, 0), Vector(50, 500), Vector(0, 500) ]
        LENGTH_14 = [ Vector(0, 0), Vector(50, 0), Vector(50, 525), Vector(0, 525) ]
        LENGTH_15 = [ Vector(0, 0), Vector(50, 0), Vector(50, 550), Vector(0, 550) ]

        # Choose two random letter to represent the two quantities
        letter1 = random.choice(['A', 'B', 'C', 'D', 'G', 'H', 'K', 'L', 'M', 'N', 'P', 'S', 'T'])
        letter2 = letter1
        while letter2 == letter1:
            letter2 = random.choice(['A', 'B', 'C', 'D', 'G', 'H', 'K', 'L', 'M', 'N', 'P', 'S', 'T'])
            
        # Standard initial positions for the shapes.
        upper_left_position = Vector(300, 300)
        lower_right_position = Vector(900, 400)
        upper_right_position = Vector(900, 300)
        lower_left_position = Vector(300, 400)
          
        # Randomize the initial positions of the shapes.
        (original_position1, original_position2) = random.choice([(upper_left_position, lower_right_position), \
            (lower_right_position, upper_left_position), \
            (upper_right_position, lower_left_position), \
            (lower_left_position, upper_right_position)])
        
        # Randomize the initial angles of the shapes.
        (original_angle1, original_angle2) = random.choice( [(0, math.pi/4), (math.pi/4, 0) , \
            (0, math.pi/4), (math.pi/4, 0), (0, math.pi/4), (math.pi/4, 0), (0, 0), (math.pi/2, 0), (0, math.pi/2) ])
        
        # The total number of problems.
        n_problems = 36
        
        # Choose a random problem.
        problem_number = random.randrange(0,n_problems)
        
        # Uncomment to test a particular problem.
        #problem_number = 0
       
        # Define the various problems. 
        if problem_number == 0:
            object1 = LENGTH_1
            object2 = LENGTH_1
    
        elif problem_number == 1:
            object1 = LENGTH_3
            object2 = LENGTH_3
    
        elif problem_number == 2:
            object1 = LENGTH_5
            object2 = LENGTH_5
    
        elif problem_number == 3:
            object1 = LENGTH_1
            object2 = LENGTH_3
        elif problem_number == 4:
            object1 = LENGTH_2
            object2 = LENGTH_4
    
        elif problem_number == 5:
            object1 = LENGTH_7
            object2 = LENGTH_7
        elif problem_number == 6:
            object1 = LENGTH_3
            object2 = LENGTH_5
        elif problem_number == 7:
            object1 = LENGTH_4
            object2 = LENGTH_6
    
        elif problem_number == 8:
            object1 = LENGTH_5
            object2 = LENGTH_7
        elif problem_number == 9:
            object1 = LENGTH_6
            object2 = LENGTH_8
    
        elif problem_number == 10:
            object1 = LENGTH_9
            object2 = LENGTH_9
        elif problem_number == 11:
            object1 = LENGTH_7
            object2 = LENGTH_9
        elif problem_number == 12:
            object1 = LENGTH_8
            object2 = LENGTH_10
    
        elif problem_number == 13:
            object1 = LENGTH_9
            object2 = LENGTH_11
        elif problem_number == 14:
            object1 = LENGTH_10
            object2 = LENGTH_12
    
        elif problem_number == 15:
            object1 = LENGTH_11
            object2 = LENGTH_11
        elif problem_number == 16:
            object1 = LENGTH_11
            object2 = LENGTH_13
        elif problem_number == 17:
            object1 = LENGTH_0
            object2 = LENGTH_2
    
        else:
            object1 = LENGTH_1
            object2 = LENGTH_1
            
        # Switch the shapes half the time (so we get > as well as < problems).
        if random.choice([0,1]) == 0:
            self.shape1 = ShapeObject(color1, letter1, object1, original_position1, original_angle1, Vector(0, 0), self)
            self.shape2 = ShapeObject(color2, letter2, object2, original_position2, original_angle2, Vector(0, 0), self)
        else:
            self.shape1 = ShapeObject(color1, letter1, object2, original_position1, original_angle1, Vector(0, 0), self)
            self.shape2 = ShapeObject(color2, letter2, object1, original_position2, original_angle2, Vector(0, 0), self)
          
        return
    
    def show_problem(self):
        self.container.add_object(self.shape1)
        self.container.add_object(self.shape2)
        
        # Randomize which  object is initially selected.
        if random.choice([0,1]) == 0:
            self.container.select_object(self.shape1)
        else:
            self.container.select_object(self.shape1)
            
        # Add letter symbols.
        self.container.letter1 = SymbolObject(Vector(500 + 400 - 50, 650), self.shape1.symbol, None, None, size=100)
        self.container.letter2 = SymbolObject(Vector(700 + 400 - 50, 650), self.shape2.symbol, None, None, size=100)

        self.container.letter1.draggable = False
        self.container.letter1.selectable = False
        self.container.letter2.draggable = False
        self.container.letter2.selectable = False

        self.container.add_object(self.container.letter1)
        self.container.add_object(self.container.letter2)

        self.container.questionmark = SymbolObject(Vector(600 + 400 - 50, 650), '?', None, None, size=80)
        self.container.questionmark.draggable = False
        self.container.questionmark.selectable = False

        self.container.add_object(self.container.questionmark)

        self.container.instructions = InstructionsObject(Vector(50, 25), 'Compare the things in length')
        self.container.add_object(self.container.instructions)
    
    def scaled(self, vectors, factor):
        for vector in vectors:
            new_vectors = [v.scaled(factor) for v in vectors]
        return new_vectors
    
    def larger(self, vectors):
        for vector in vectors:
            new_vectors = [v.scaled(1.2) for v in vectors]
        return new_vectors
    
    def smaller(self, vectors):
        for vector in vectors:
            new_vectors = [v.scaled(0.8) for v in vectors]
        return new_vectors
    
    def check_problem_solved(self):
        #print "Length Problem: check_problem_solved called"
        # Make sure the two ShapeObjects both have four points.
        if len(self.shape1.points) != 4 or len(self.shape2.points) != 4:
            return False
        
        # First, find out how many points coincide.    
        p0 = self.shape1.points
        p0 = [self.shape1.transform_point(p) for p in p0]

        p1 = self.shape2.points
        p1 = [self.shape2.transform_point(p) for p in p1]
        
        # Sort the points so they can be compared consistently.
        def sort_points_arbitrarily(a, b):
            if a.x != b.x:   
                return cmp(a.x, b.x)
            else:
                return cmp(a.y, b.y)

        p0 = sorted(p0, cmp=sort_points_arbitrarily)
        p1 = sorted(p1, cmp=sort_points_arbitrarily)

        n_equal = 0
        for i in range(0,len(p0)):
            for j in range(0,len(p1)):
                #print "p0[i] =", p0[i]
                #print "p1[j] =", p1[j]
                if p0[i].approx_equal(p1[j]):
                    n_equal += 1
                
        #print "n_equal =", n_equal

        if self.answer == 'equal' and n_equal == 2:
            return True
        elif (self.answer == 'less' or self.answer == 'greater') and n_equal == 1:
            return True

        # Test if one object is completely inside the other (areas are not equal)
        #area0 = self.shape1.area
        #area1 = self.shape2.area
        # 
        #if area0 > area1:
        #    object_larger = self.shape1
        #    p_smaller = p1
        #else:
        #    object_larger = self.shape2
        #    p_smaller = p0
        #
        #for i in range(0,len(self.shape1.points)):
        #    if not object_larger.contains_point(p_smaller[i]):
        #        return False
                                 
        return False
    
    def find_answer(self):
        if self.shape1.area > self.shape2.area:
            self.answer = 'greater'
        elif self.shape1.area < self.shape2.area:
            self.answer = 'less'
        else:
            self.answer = 'equal'
        
        return self.answer