New buildings, trees, and math functions

New buildings, trees, and math functions

Overview

  • index.html: Added scripts for building.js and tree.js.
  • js/items/building.js: New class Building for drawing buildings.
  • js/items/tree.js: New class Tree for drawing trees.
  • js/math/graph.js: Added static method load() for loading graph info.
  • js/math/utils.js: Added functions lerp2D(), getFake3dPoint(), and getRandomColor().
  • js/primitives/polygon.js: Added join option to draw() method.
  • js/world.js: Changed representation of buildings, added trees, changed how they are drawn and added a viewPoint parameter to the draw(ctx, viewPoint) method.

File wise changes made

index.html

  • Added the scripts js/items/tree.js and js/items/building.js in the <head> tag
  • Removed world.draw(ctx); and added the following code:
    • Created a variable viewPoint
    • Added world.draw(ctx, viewPoint);
    • Added ctx.globalAlpha = 0.3;
    • Added graphEditor.display();
    • Added requestAnimationFrame(animate);

js/items/building.js

  • Added a new class called Building which takes a polygon and a height as parameters
  • Building has a draw() method that draws the building in the context provided
  • The draw() method creates the base, the sides, the ceiling, the roof, and the top midpoints of the building
  • It then draws the base, the sides, the ceiling, and the roof of the building

js/items/tree.js

  • Added a new class called Tree which takes a center, a size, and a height as parameters
  • Tree has a draw() method that draws the tree in the context provided
  • The draw() method generates a base polygon for the tree, and then draws it
  • It then creates a top point for the tree, and draws the trunk of the tree
  • It then draws the leaves of the tree using a loop that creates a series of polygons and draws them

js/math/graph.js

  • Added a static method called "load" which takes a Graph object as a parameter
  • The "load" method creates a new Graph object with the points and segments from the JSON object
  • It then returns the new Graph object
  • Added a "hash" method to the Graph class which returns a JSON string representation of the Graph object

js/math/utils.js

  • Added a function called lerp2D which takes two points and a value between 0 and 1 as parameters
  • The lerp2D function returns a new point which is the linear interpolation between the two points, with the value t specifying the weighting between the two points
  • Added a function called getFake3dPoint which takes a point, a view point, and a height as parameters
  • The getFake3dPoint function returns a new point which is the 3D projection of the point onto the plane defined by the view point and the height

js/primitives/polygon.js

  • Added a join parameter to the draw method of the Polygon class
  • The join parameter specifies the type of join to be used when drawing the polygon

js/world.js

  • Changed ...this.buildings.map((b) => b.points).flat() to ...this.buildings.map((b) => b.base.points).flat() in the #generateTrees() method
  • Added viewPoint as a parameter to the draw method of the World class
  • Changed for (const tree of this.trees) to items.sort(...) and for (const item of items) in the draw method of the World class
  • Changed tree.draw(ctx, { size: this.treeSize, color: "rgba(0,0,0,0.5)" }); to item.draw(ctx, viewPoint); in the draw method of the World class
  • Changed for (const bld of this.buildings) { bld.draw(ctx); } to for (const bld of this.buildings) { bld.draw(ctx); } in the draw method of the World class

Summary

  • index.html:
    • Added