This commit is contained in:
mtgmonkey 2025-06-12 06:40:52 -04:00
parent 361c7a8734
commit bd377d0dc6

View file

@ -8,6 +8,7 @@ import Tuple exposing (first, second)
type alias Memory = type alias Memory =
{ dx : Number { dx : Number
, count : Int
} }
@ -26,6 +27,7 @@ type alias Coords =
init : Memory init : Memory
init = init =
{ dx = 0.8 { dx = 0.8
, count = 0
} }
@ -53,6 +55,11 @@ view computer memory =
placeCornerAtCoords computer bounds placeCornerAtCoords computer bounds
fx x = x * x * x / 32 / 32 fx x = x * x * x / 32 / 32
corner = case memory.count // 30 of
0 ->
TopLeft
1 -> TopRight
_ -> TopLeft
in in
List.append List.append
(makeRectangles computer atCorner fx bounds memory []) (makeRectangles computer atCorner fx bounds memory [])
@ -60,12 +67,59 @@ view computer memory =
List.append List.append
(makeCircles computer fx bounds memory []) (makeCircles computer fx bounds memory [])
([ P.words black "Welcome to my math project! Use arrow keys to adjust the width of each rectangle." ([ P.words black "Welcome to my math project! Use arrow keys to adjust the width of each rectangle."
|> at ( (bounds.xmax - bounds.xmin) / 2, bounds.ymax - 1) |> at ( (bounds.xmax - bounds.xmin) / 2, bounds.ymax - 0.5)
, P.words black ("The current dx is " ++ (String.fromFloat memory.dx)) , P.words black ("The current dx is " ++ (String.fromFloat memory.dx))
|> at ( (bounds.xmax - bounds.xmin) / 2, bounds.ymax - 1)
, P.words black ("The current approximation is " ++ (String.fromFloat <| (\n->n/32/32) <| getArea computer corner fx bounds memory))
|> at ( (bounds.xmax - bounds.xmin) / 2, bounds.ymax - 1.5)
, P.words black ("The real area under the curve is thus about " ++ (String.fromFloat 898))
|> at ( (bounds.xmax - bounds.xmin) / 2, bounds.ymax - 2) |> at ( (bounds.xmax - bounds.xmin) / 2, bounds.ymax - 2)
]) ])
fillLeftFxs : Computer -> (Number -> Number) -> Bounds -> Memory -> List Number -> List Number
fillLeftFxs computer fx bounds memory fxs =
let
xcoord = toFloat (List.length fxs) * memory.dx
ycoord = fx xcoord
widthOfCell = computer.screen.width / (bounds.xmax - bounds.xmin)
newVal = memory.dx * widthOfCell * ycoord * widthOfCell
in
if memory.dx + memory.dx * (toFloat <| List.length fxs) < bounds.xmax then
fillLeftFxs computer fx bounds memory <| List.append fxs <| [newVal]
else
List.append fxs <| [(bounds.xmax - memory.dx * (toFloat <| List.length fxs)) * widthOfCell * ycoord * widthOfCell]
fillRightFxs : Computer -> (Number -> Number) -> Bounds -> Memory -> List Number -> List Number
fillRightFxs computer fx bounds memory fxs =
let
xcoord = toFloat (List.length fxs) * memory.dx
ycoord = fx <| memory.dx + xcoord
widthOfCell = computer.screen.width / (bounds.xmax - bounds.xmin)
newVal = memory.dx * widthOfCell * ycoord * widthOfCell
in
if memory.dx + memory.dx * (toFloat <| List.length fxs) < bounds.xmax then
fillRightFxs computer fx bounds memory <| List.append fxs <| [newVal]
else
List.append fxs <| [(bounds.xmax - memory.dx * (toFloat <| List.length fxs)) * widthOfCell * ycoord * widthOfCell]
getArea : Computer -> Corner -> (Number -> Number) -> Bounds -> Memory -> Number
getArea computer corner fx bounds memory =
let
fxs0 = fillLeftFxs computer fx bounds memory []
fxs1 = fillRightFxs computer fx bounds memory []
in
case corner of
TopLeft ->
List.sum fxs0
TopRight ->
List.sum fxs1
makeCircles : Computer -> (Number -> Number) -> Bounds -> Memory -> List Shape -> List Shape makeCircles : Computer -> (Number -> Number) -> Bounds -> Memory -> List Shape -> List Shape
makeCircles computer fx bounds memory prev = makeCircles computer fx bounds memory prev =
@ -92,12 +146,19 @@ makeRectangles computer atCorner fx bounds memory prev =
xcoord = toFloat (List.length prev) * memory.dx xcoord = toFloat (List.length prev) * memory.dx
ycoord = fx xcoord ycoord = fx xcoord
widthOfCell = computer.screen.width / (bounds.xmax - bounds.xmin) widthOfCell = computer.screen.width / (bounds.xmax - bounds.xmin)
corner = case (memory.count // 30) of
0 ->
TopLeft
1 ->
TopRight
_ ->
TopLeft
newRec = newRec =
rectangle red (memory.dx * widthOfCell) (ycoord * widthOfCell) rectangle red (memory.dx * widthOfCell) (ycoord * widthOfCell)
|> atCorner (xcoord, ycoord) TopLeft (memory.dx, ycoord) |> atCorner (xcoord, ycoord) corner (memory.dx, ycoord)
in in
if if
xcoord < bounds.xmax xcoord - memory.dx < bounds.xmax
then then
makeRectangles computer atCorner fx bounds memory <| List.append prev [ newRec ] makeRectangles computer atCorner fx bounds memory <| List.append prev [ newRec ]
@ -125,6 +186,14 @@ update computer memory =
|> (*) step |> (*) step
|> (+) threshold |> (+) threshold
)) / toFloat 100 )) / toFloat 100
, count =
if memory.count <= 0 then
memory.count + 50
else
if memory.count >= 60 then
memory.count - 50
else
(+) memory.count <| round <| toY computer.keyboard
} }
@ -150,7 +219,7 @@ placeAtCoords computer bounds (x, y) shape =
shape shape
type Corner = TopLeft | TopRight | BottomLeft | BottomRight type Corner = TopLeft | TopRight
placeCornerAtCoords : Computer -> Bounds -> Coords -> Corner -> (Number, Number) -> Shape -> Shape placeCornerAtCoords : Computer -> Bounds -> Coords -> Corner -> (Number, Number) -> Shape -> Shape
placeCornerAtCoords computer bounds (x, y) corner (width, height) shape = placeCornerAtCoords computer bounds (x, y) corner (width, height) shape =
@ -161,8 +230,4 @@ placeCornerAtCoords computer bounds (x, y) corner (width, height) shape =
(x + width / 2, y - height / 2) (x + width / 2, y - height / 2)
TopRight -> TopRight ->
(x - width / 2, y - height / 2) (x - width / 2, y - height / 2)
BottomLeft ->
(x + width / 2, y + height / 2)
BottomRight ->
(x - width / 2, y + height / 2)
) )