-- CIS 252, Homework 2 -- VERSION 2, Wed Jan 30 -- Name: FILL-IN -- Email: FILL-IN ----------------------------------------------------------------------- ----------------------------------------------------------------------- -- Problem 1 ----------------------------------------------------------------------- -- CONTRACT distance :: (Float,Float) -> (Float,Float) -> Float -- PURPOSE -- distance (x1,y1) (x2,y2) -- returns the Euclidian distance between points (x1,y1) and (x2,y2) -- EXAMPLES -- distance (0,0) (1,0) -- should be 1 -- distance (0,1) (1,1) -- should be about 1.414214 -- distance (-1,-1) (1,2) -- should be about 3.605551 -- **ADD MORE** -- DEFINITION distance (x1,y1) (x2,y2) = error "Change me!" -- <<<<< CHANGE THIS <<<<< -- TESTS ans1a = distance (0,0) (1,0) -- should be 1 ans1b = distance (0,1) (1,1) -- should be about 1.414214 ans1c = distance (-1,-1) (1,2) -- should be about 3.605551 -- **ADD MORE** ----------------------------------------------------------------------- ----------------------------------------------------------------------- -- Problem 2 ----------------------------------------------------------------------- -- CONTRACT tcDistance :: (Float,Float) -> (Float,Float) -> Float -- PURPOSE -- tcDistance (x1,y1) (x2,y2) -- returns the taxi-cab distance between points (x1,y1) and (x2,y2) -- EXAMPLES -- distance (0,0) (1,0) -- should be 1 -- distance (0,1) (1,1) -- should be 2 -- distance (-1,-1) (1,2) -- should be about 3.605551 -- **ADD MORE** -- DEFINITION tcDistance (x1,y1) (x2,y2) = error "Change me!" -- <<<<< CHANGE THIS <<<<< -- TESTS ans2a = tcDistance (0,1) (1,1) -- should be 1 ans2b = tcDistance (0,1) (1,1) -- should be 2 ans2c = tcDistance (-1,-1) (1,2) -- should be 5 -- **ADD MORE** ----------------------------------------------------------------------- ----------------------------------------------------------------------- -- Problem 3 ----------------------------------------------------------------------- -- CONTRACT insideCircle :: (Float,Float) -> (Float,Float) -> Float -> Bool -- PURPOSE -- insideCircle (a,b) (x,y) r -- tests whether the point (a,b) is inside the circle having origin -- (x,y) and radius r. -- EXAMPLES -- insideCircle (0,0) (1,1) 1 -- should be False -- insideCircle (0,0) (1,1) 2 -- should be True -- ** ADD MORE ** -- DEFINITION insideCircle (a,b) (x,y) r = error "Change me!" -- <<<<< CHANGE THIS <<<<< -- TESTS ans3a = insideCircle (0,0) (1,1) 1 -- should be False ans3b = insideCircle (0,0) (1,1) 2 -- should be True -- ** ADD MORE ** ----------------------------------------------------------------------- ----------------------------------------------------------------------- -- Problem 4 ----------------------------------------------------------------------- -- CONTRACT insideDiamond :: (Float,Float) -> (Float,Float) -> Float -> Bool -- PURPOSE -- insideCircle (a,b) (x,y) r -- tests whether the point (a,b) is inside the diamond having origin -- (x,y) and radius r. -- EXAMPLES -- insideDiamond (0,0) (1,1) 1 -- should be True -- insideDiamond (0,0) (1,1) 2 -- should be True -- ** ADD MORE ** -- DEFINITION insideDiamond (a,b) (x,y) r = error "Change me!" -- <<<<< CHANGE THIS <<<<< -- TESTS ans4a = insideDiamond (0,0) (1,1) 1 -- should be False ans4b = insideDiamond (0,0) (1,1) 2 -- should be True ----------------------------------------------------------------------- ----------------------------------------------------------------------- -- Problem 5 ----------------------------------------------------------------------- -- CONTRACT nestedCircle :: (Float,Float) -> Float -> (Float,Float) -> Float -> Bool -- PURPOSE -- nestedCircle (x1,y1) r1 (x2,y2) r2 -- tests whether the circle having origin (x1,y1) and radius r1 is -- completely inside the circle having origin (x2,y2) and radius r2. -- EXAMPLES -- nestedCircle (3,4) 1 (0,0) 5 -- should return False -- nestedCircle (2,2) 1 (0,0) 5 -- should return True -- ****ADD MORE**** -- DEFINITION nestedCircle (x1,y1) r1 (x2,y2) r2 = error "Change me!" -- << CHANGE THIS << -- TESTS ans5a = nestedCircle (3,4) 1 (0,0) 5 -- should return False ans5b = nestedCircle (2,2) 1 (0,0) 5 -- should return True -- ****ADD MORE**** ----------------------------------------------------------------------- ----------------------------------------------------------------------- -- Problem 6 ----------------------------------------------------------------------- -- CONTRACT nestedDiamond :: (Float,Float) -> Float -> (Float,Float) -> Float -> Bool -- PURPOSE -- nestedDiamond (x1,y1) r1 (x2,y2) r2 -- tests whether the diamond having origin (x1,y1) and radius r1 is -- completely inside the diamond having origin (x2,y2) and radius r2. -- EXAMPLES -- nestedDiamond (3,4) 1 (0,0) 5 -- should return False -- nestedDiamond (0,0) 1 (0,0) 5 -- should return True -- ****ADD MORE**** -- DEFINITION nestedDiamond (x1,y1) r1 (x2,y2) r2 = error "Change me!" -- << CHANGE THIS << -- TESTS ans6a = nestedDiamond (3,4) 1 (0,0) 5 -- should return False ans6b = nestedDiamond (0,0) 1 (0,0) 5 -- should return True -- ****ADD MORE**** ----------------------------------------------------------------------- ----------------------------------------------------------------------- -- Problem 7 ----------------------------------------------------------------------- -- CONTRACT circlesTouch :: (Float,Float) -> Float -> (Float,Float) -> Float -> Bool -- PURPOSE -- circlesTouch (x1,y1) r1 (x2,y2) r2 -- determines whether the circle having origin (x1,y1) and radius r1 -- is touching the circle having origin (x2,y2) and radius r2 -- EXAMPLES -- (circlesTouch (3,4) 1 (0,0) 5) should return True -- (circlesTouch (3,4) 1 (0,0) 2) should return False -- ****ADD MORE**** -- DEFINITION circlesTouch (x1,y1) r1 (x2,y2) r2 = error "Change me!" -- << CHANGE THIS << -- TESTS ans7a = circlesTouch (3,4) 1 (0,0) 5 -- should be True ans7b = circlesTouch (3,4) 1 (0,0) 2 -- should be False -- ****ADD MORE**** ----------------------------------------------------------------------- ----------------------------------------------------------------------- -- Problem 8 ----------------------------------------------------------------------- -- CONTRACT diamondsTouch :: (Float,Float) -> Float -> (Float,Float) -> Float -> Bool -- PURPOSE -- diamondsTouch (x1,y1) r1 (x2,y2) r2 -- determines whether the diamond having origin (x1,y1) and radius -- r1 is touching the diamond having origin (x2,y2) and radius r2 -- EXAMPLES -- (diamondsTouch (3,4) 1 (0,0) 5) should return False -- (diamondsTouch (3,4) 1 (1,1) 5) should return True -- ****ADD MORE**** -- DEFINITION diamondsTouch (x1,y1) r1 (x2,y2) r2 = error "Change me!" -- << CHANGE THIS << -- TESTS ans8a = diamondsTouch (3,4) 1 (0,0) 5 -- should be False ans8b = diamondsTouch (3,4) 1 (1,1) 5 -- should be True -- ****ADD MORE**** ----------------------------------------------------------------------- ----------------------------------------------------------------------- -- Problem 9 ----------------------------------------------------------------------- -- CONTRACT diamondCoord :: (Float,Float) -> Float -> Int -> (Float,Float) -- PURPOSE -- diamondCoord (x,y) r n -- returns the coordinates of the n-th base of the diamond with -- origin (x,y) and radius r. (1 == 1st base, 2 = 2nd base, -- 3 == 3rd base, 4 == home base). -- EXAMPLES -- diamondCoord (0,0) 1 0 -- should return (0,-1) -- diamondCoord (0,0) 1 1 -- should return (1,0) -- diamondCoord (0,0) 1 2 -- should return (0,1) -- diamondCoord (0,0) 1 3 -- should return (-1,0) -- ****ADD MORE**** -- DEFINITION diamondCoord (x,y) r n = error "Change me!" -- <<<<< CHANGE THIS <<<<< -- TESTS ans9a = diamondCoord (0,0) 1 0 -- should return (0,-1) ans9b = diamondCoord (0,0) 1 1 -- should return (1,0) ans9c = diamondCoord (0,0) 1 2 -- should return (0,1) ans9d = diamondCoord (0,0) 1 3 -- should return (-1,0) -- ****ADD MORE**** ----------------------------------------------------------------------- ----------------------------------------------------------------------- -- Problem 10 ----------------------------------------------------------------------- -- CONTRACT insideDC :: (Float,Float) -> Float -> (Float,Float) -> Float -> Bool -- PURPOSE -- insideDC (x1,y1) r1 (x2,y2) r2 -- determines whether the diamond having origin (x1,y1) and radius -- r1 is completely inside the circle having origin (x2,y2) and -- radius r2. -- EXAMPLES -- insideDC (0,0) 1 (1,0) 4 -- should return True -- insideDC (0,0) 1 (1,0) 2 -- should return True -- ****ADD MORE**** -- DEFINITION insideDC (x1,y1) r1 (x2,y2) r2 = error "Change me!" -- <<<<< CHANGE THIS <<<<< -- TESTS ans10a = insideDC (0,0) 1 (1,0) 4 -- should return True ans10b = insideDC (0,0) 1 (1,0) 2 -- should return True -- ****ADD MORE**** ----------------------------------------------------------------------- ----------------------------------------------------------------------- -- CUT THE FOLLOWING OUT IN THE SUBMITTED HOMEWORK. -- -- sqrt is a function that computes the square root of a number -- abs is a function that computes the absolute value of a number -- x^2 is the square of x -- Here are some sample uses of the above. -- anyRoots a b c -- tests if a*x^2+b*x+c has any real roots anyRoots :: Float -> Float -> Float -> Bool anyRoots a b c = (b^2-4*a*c >=0) -- roots a b c -- returns the real roots of a*x^2+b*x+c, if it has any, -- and causes an error a*x^2+b*x+c has no real roots roots :: Float -> Float -> Float -> (Float,Float) roots a b c | anyRoots a b c = ( (-b-sqrt(b^2-4*a*c))/(2*a), (-b+sqrt(b^2-4*a*c))/(2*a) ) | otherwise = error "no real roots" -- difference a b -- returns how far apart the values a and b are. -- E.g. (differnce 3 5) is 2 -- (differnce 5 3) is also 2. difference :: Float -> Float -> Float difference a b = abs(a - b)