basic functionality

This commit is contained in:
mtgmonkey 2025-06-11 21:46:58 -04:00
parent 59a816b96f
commit 361c7a8734
3 changed files with 100 additions and 41 deletions

View file

@ -27,7 +27,6 @@ in
buildPhase = '' buildPhase = ''
elm make ./Main.elm --optimize --output=tmp.js elm make ./Main.elm --optimize --output=tmp.js
esbuild ./tmp.js --minify --target=es5 --outfile=main.js esbuild ./tmp.js --minify --target=es5 --outfile=main.js
echo "<!DOCTYPE html><html><head><script type = 'text/javascript' src = './main.js'></script></head><body><script type = 'text/javascript' src = './init.js'></script></body></html>" > index.html
''; '';
installPhase = '' installPhase = ''
mkdir $out/bin -p mkdir $out/bin -p
@ -41,7 +40,6 @@ in
cp index.html $out/src/index.html cp index.html $out/src/index.html
cp main.js $out/src/main.js cp main.js $out/src/main.js
cp init.js $out/src/init.js
''; '';
meta = { meta = {
mainProgram = "math-project"; mainProgram = "math-project";

View file

@ -25,7 +25,7 @@ type alias Coords =
init : Memory init : Memory
init = init =
{ dx = 100 { dx = 0.8
} }
@ -41,47 +41,65 @@ view computer memory =
let let
bounds = bounds =
{ ymin = 0 { ymin = 0
, ymax = 511 , ymax = 32 * computer.screen.height / computer.screen.width
, xmin = 0 , xmin = 0
, xmax = 511 * computer.screen.width / computer.screen.height , xmax = 32
} }
at = at =
placeAtCoords computer bounds placeAtCoords computer bounds
fx x = atCorner =
(x - (bounds.xmax - bounds.xmin) / 2) ^ 2 / 1000 placeCornerAtCoords computer bounds
fx x = x * x * x / 32 / 32
in in
List.append List.append
[ P.words purple "Welcome to my math project! Use arrow keys to adjust the width of each rectangle." (makeRectangles computer atCorner fx bounds memory [])
|> at ( (bounds.xmax - bounds.xmin) / 2, bounds.ymax - 10 ) <|
, P.words purple "The current equation is y=0.25x^2." List.append
|> at ( (bounds.xmax - bounds.xmin) / 2, bounds.ymax - 20 ) (makeCircles computer fx bounds memory [])
, P.words purple ("The current dx is " ++ (String.fromInt <| truncate memory.dx)) ([ 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 - 30 ) |> at ( (bounds.xmax - bounds.xmin) / 2, bounds.ymax - 1)
, P.square purple 10 , P.words black ("The current dx is " ++ (String.fromFloat memory.dx))
|> at ( 100, 100 ) |> at ( (bounds.xmax - bounds.xmin) / 2, bounds.ymax - 2)
] ])
<|
makeRectangles at fx bounds memory []
makeRectangles : (Coords -> Shape -> Shape) -> (Number -> Number) -> Bounds -> Memory -> List Shape -> List Shape
makeRectangles at fx bounds memory prev = makeCircles : Computer -> (Number -> Number) -> Bounds -> Memory -> List Shape -> List Shape
makeCircles computer fx bounds memory prev =
let let
height = resolution = (bounds.xmax - bounds.xmin) / 512
fx <| toFloat (List.length prev + 1) * memory.dx xcoord = toFloat (List.length prev) * resolution
ycoord = fx xcoord
numOfRecs = newCirc =
1 + (truncate <| toFloat (List.length prev) * memory.dx) circle purple 2
|> placeAtCoords computer bounds (xcoord, ycoord)
newRec =
rectangle red memory.dx height
|> at ( memory.dx / 4 + memory.dx * toFloat (List.length prev) / 2, height / 4 )
in in
if toFloat (List.length prev) <= 1 + 2 * (bounds.xmax - bounds.xmin) / memory.dx then if
makeRectangles at fx bounds memory <| xcoord < bounds.xmax
List.append prev [ newRec ] then
makeCircles computer fx bounds memory <| List.append prev [ newCirc ]
else
prev
makeRectangles : Computer -> (Coords -> Corner -> (Number, Number) -> Shape -> Shape) -> (Number -> Number) -> Bounds -> Memory -> List Shape -> List Shape
makeRectangles computer atCorner fx bounds memory prev =
let
xcoord = toFloat (List.length prev) * memory.dx
ycoord = fx xcoord
widthOfCell = computer.screen.width / (bounds.xmax - bounds.xmin)
newRec =
rectangle red (memory.dx * widthOfCell) (ycoord * widthOfCell)
|> atCorner (xcoord, ycoord) TopLeft (memory.dx, ycoord)
in
if
xcoord < bounds.xmax
then
makeRectangles computer atCorner fx bounds memory <| List.append prev [ newRec ]
else else
prev prev
@ -89,8 +107,24 @@ makeRectangles at fx bounds memory prev =
update : Computer -> Memory -> Memory update : Computer -> Memory -> Memory
update computer memory = update computer memory =
let
step = 0.02
threshold = 0.02
in
{ memory { memory
| dx = memory.dx + 0.2 * toX computer.keyboard | dx =
(toFloat
<| round
<| toFloat 100 * (
if memory.dx > threshold then
memory.dx + step * toX computer.keyboard
else
computer.keyboard
|> toX
|> max 0
|> (*) step
|> (+) threshold
)) / toFloat 100
} }
@ -107,12 +141,28 @@ placeAtScreen computer coords shape =
placeAtCoords : Computer -> Bounds -> Coords -> Shape -> Shape placeAtCoords : Computer -> Bounds -> Coords -> Shape -> Shape
placeAtCoords computer bounds coords shape = placeAtCoords computer bounds (x, y) shape =
let placeAtScreen
x = computer
first coords ( computer.screen.width * x / (bounds.xmax - bounds.xmin)
, computer.screen.height * y / (bounds.ymax - bounds.ymin)
)
shape
y =
second coords type Corner = TopLeft | TopRight | BottomLeft | BottomRight
in
placeAtScreen computer ( x / (bounds.xmax - bounds.xmin) * computer.screen.width, y / (bounds.ymax - bounds.ymin) * computer.screen.height ) shape placeCornerAtCoords : Computer -> Bounds -> Coords -> Corner -> (Number, Number) -> Shape -> Shape
placeCornerAtCoords computer bounds (x, y) corner (width, height) shape =
shape
|> placeAtCoords computer bounds
(case corner of
TopLeft ->
(x + width / 2, y - height / 2)
TopRight ->
(x - width / 2, y - height / 2)
BottomLeft ->
(x + width / 2, y + height / 2)
BottomRight ->
(x - width / 2, y + height / 2)
)

11
src/index.html Normal file
View file

@ -0,0 +1,11 @@
<!DOCTYPE html>
<html>
<head>
<script type = 'text/javascript' src = './main.js'></script>
</head>
<body>
<script>
var app = Elm.Main.init();
</script>
</body>
</html>