Skip to content
This repository was archived by the owner on Jul 24, 2023. It is now read-only.

Commit bd911ad

Browse files
committed
complete fill up
1 parent dc43ad1 commit bd911ad

File tree

1 file changed

+35
-22
lines changed

1 file changed

+35
-22
lines changed

src/Recursion.hs

+35-22
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,22 @@ genPerm' = genPermHelper [[]]
375375
editing programs. That is, given a screen (represented by a
376376
two-dimensional array of colors), a point, and a new color, fill in
377377
the surrounding area until the color changes from the original color.
378+
379+
test case: fillUpColor image (0,0) Blue
380+
381+
original image:
382+
[
383+
[Red,Red,Red],
384+
[Red,Yellow,Red],
385+
[Yellow,Yellow,Blue]
386+
]
387+
388+
expect image:
389+
[
390+
[Blue,Blue,Blue],
391+
[Blue,Yellow,Blue],
392+
[Yellow,Yellow,Blue]
393+
]
378394
-}
379395

380396
data Color = Red | Yellow | Blue deriving Show
@@ -402,30 +418,27 @@ image = V.fromList
402418
V.fromList [Yellow, Yellow, Blue]
403419
]
404420

405-
image1 :: Image
406-
image1 = V.fromList
407-
[
408-
V.fromList [Red, Red],
409-
V.fromList [Red, Yellow]
410-
]
421+
fillUpColor :: Image -> (Int, Int) -> Color -> Image
422+
fillUpColor img (i,j) c = foldl (\acc x -> paint acc x c ) img pList
423+
where pList = findArea img (i,j)
411424

412425
-- Paint a color in one location
413426
paint :: Image -> (Int, Int) -> Color -> Image
414-
paint vs (i, j) c =
427+
paint vs (i,j) c =
415428
fstHVects V.++ V.fromList[newPaintRow] V.++ V.drop 1 secHVects
416-
where
429+
where
417430
(fstHVects, secHVects) = V.splitAt i vs
418431
(fstHPaintRow, secHPaintRow) = V.splitAt j (vs V.! i)
419-
newPaintRow =
432+
newPaintRow =
420433
fstHPaintRow V.++ V.fromList[c] V.++ V.drop 1 secHPaintRow
421434

422435
-- Find all locations which need to paint
423436
findArea :: Image -> (Int, Int) -> [(Int, Int)]
424437
findArea img (i,j) = uniq (
425438
(i,j):
426-
findAreaOnDir img (i,j) boundC Up ++
427-
findAreaOnDir img (i,j) boundC Down ++
428-
findAreaOnDir img (i,j) boundC PLeft ++
439+
findAreaOnDir img (i,j) boundC Up ++
440+
findAreaOnDir img (i,j) boundC Down ++
441+
findAreaOnDir img (i,j) boundC PLeft ++
429442
findAreaOnDir img (i,j) boundC PRight) []
430443
where boundC = img V.! i V.! j
431444

@@ -441,31 +454,31 @@ findAreaOnDir img (i,j) c Up
441454
(i,j-1): findAreaOnDir img (i,j-1) c PLeft
442455
| isInBoundAndSameColor img (i-1,j) c =
443456
(i-1,j): findAreaOnDir img (i-1,j) c Up
444-
| isInBoundAndSameColor img (i,j+1) c =
457+
| isInBoundAndSameColor img (i,j+1) c =
445458
(i,j+1): findAreaOnDir img (i,j+1) c PRight
446459
| otherwise = []
447460
findAreaOnDir img (i,j) c Down
448-
| isInBoundAndSameColor img (i,j-1) c =
461+
| isInBoundAndSameColor img (i,j-1) c =
449462
(i,j-1): findAreaOnDir img (i,j-1) c PLeft
450463
| isInBoundAndSameColor img (i+1,j) c =
451464
(i+1,j): findAreaOnDir img (i+1,j) c Down
452-
| isInBoundAndSameColor img (i,j+1) c =
465+
| isInBoundAndSameColor img (i,j+1) c =
453466
(i,j+1): findAreaOnDir img (i,j+1) c PRight
454467
| otherwise = []
455468
findAreaOnDir img (i,j) c PLeft
456-
| isInBoundAndSameColor img (i-1,j) c =
469+
| isInBoundAndSameColor img (i-1,j) c =
457470
(i-1,j): findAreaOnDir img (i-1,j) c Up
458-
| isInBoundAndSameColor img (i,j-1) c =
471+
| isInBoundAndSameColor img (i,j-1) c =
459472
(i,j-1): findAreaOnDir img (i,j-1) c PLeft
460-
| isInBoundAndSameColor img (i+1,j) c =
473+
| isInBoundAndSameColor img (i+1,j) c =
461474
(i+1,j): findAreaOnDir img (i+1,j) c Down
462475
| otherwise = []
463476
findAreaOnDir img (i,j) c PRight
464-
| isInBoundAndSameColor img (i-1,j) c =
477+
| isInBoundAndSameColor img (i-1,j) c =
465478
(i-1,j): findAreaOnDir img (i-1,j) c Up
466-
| isInBoundAndSameColor img (i,j+1) c =
479+
| isInBoundAndSameColor img (i,j+1) c =
467480
(i,j+1): findAreaOnDir img (i,j+1) c PRight
468-
| isInBoundAndSameColor img (i+1,j) c =
481+
| isInBoundAndSameColor img (i+1,j) c =
469482
(i+1,j): findAreaOnDir img (i+1,j) c Down
470483
| otherwise = []
471484

@@ -474,7 +487,7 @@ isInBoundAndSameColor img (i,j) c = isInBound img (i,j) && selectC == c
474487
where selectC = img V.! i V.! j
475488

476489
isInBound :: Image -> (Int, Int) -> Bool
477-
isInBound img (i,j)
490+
isInBound img (i,j)
478491
| (0 <= i && i < xBound) && (0 <= j && j < yBound) = True
479492
| otherwise = False
480493
where xBound = length img

0 commit comments

Comments
 (0)