Added Tree Generation and Buildings
Overview
The following changes have been made:
- Added methods to compute the hash, dot product, normalize, magnitude of points.
- Added
distanceToPoint
,projectPoint
,length
,directionVector
methods to line segments. - Added methods to compute the distance and intersection of polygons and segments.
- Added more sophisticated functions to generate buildings, trees, and road borders in the
World
class.
File wise changes made
index.html
- Added a check to see if the graph hash has changed since the last time it was rendered.
- If the graph hash has changed, regenerate the world and update the graph hash.
js/math/graph.js
- Added a
hash()
method to the Graph class that returns the JSON string representation of the graph.
js/math/utils.js
- Added
dot()
function to calculate the dot product of two points. - Added
normalize()
function to normalize a point. - Added
magnitude()
function to calculate the magnitude of a point. - Added
translate()
function to translate a point by a given angle and offset.
js/primitives/polygon.js
- Added
distanceToPoint()
method to calculate the minimum distance from a polygon to a point. - Added
distanceToPoly()
method to calculate the minimum distance from a polygon to another polygon. - Added
intersectsPoly()
method to check if a polygon intersects with another polygon. - Added
containsSegment()
method to check if a polygon contains a segment.
js/primitives/segment.js
- Added
length()
method to calculate the length of a segment. - Added
directionVector()
method to calculate the direction vector of a segment. - Added
distanceToPoint()
method to calculate the minimum distance from a segment to a point. - Added
projectPoint()
method to project a point onto a segment.
js/world.js
- Added
roadWidth
,roadRoundness
,buildingWidth
,buildingMinLength
,spacing
, andtreeSize
arguments to theWorld
constructor. - Added
#generateTrees()
method to generate trees in the world. - Added
#generateBuildings()
method to generate buildings in the world. - Added
trees
andbuildings
arrays to store the trees and buildings in the world. - Updated the
draw()
method to draw the trees and buildings.
Summary
- A new function
hash
was added to thejs/math/graph
file. This function converts a graph object into a unique string representation, which can be used to quickly check if two graphs are identical. - A new function
dot
was added to thejs/math/utils
file. This function computes the dot product of two points, which is used in various geometric calculations. - A new function
normalize
was added to thejs/math/utils
file. This function normalizes a vector to have a length of 1, which is useful for representing directions. - A new function
magnitude
was added to thejs/math/utils
file. This function computes the magnitude (length) of a vector. - A new function
distanceToPoint
was added to thejs/primitives/polygon
file. This function computes the minimum distance between a point and the polygon. - A new function
distanceToPoly
was added to thejs/primitives/polygon
file. This function computes the minimum distance between two polygons. - A new function
intersectsPoly
was added to thejs/primitives/polygon
file. This function checks if two polygons intersect. - A new function
length
was added to thejs/primitives/segment
file. This function computes the length of the segment. - A new function
directionVector
was added to thejs/primitives/segment
file. This function computes a unit vector that points in the direction of the segment. - A new function
distanceToPoint
was added to thejs/primitives/segment
file. This function computes the minimum distance between a point and the segment. - A new function
projectPoint
was added to thejs/primitives/segment
file. This function projects a point onto the segment, which is useful for finding the closest point on the segment to the given point. - The
js/world
file was significantly rewritten. A new constructor was added that takes several additional parameters, including the size of buildings, the minimum length of buildings, the spacing between objects, and the size of trees. - A new function
#generateTrees
was added to thejs/world
file. This function generates a random distribution of trees in the world, avoiding placing trees inside or too close to buildings or roads. - A new function
#generateBuildings
was added to thejs/world
file. This function generates a random distribution of buildings in the world, avoiding placing buildings inside or too close to each other or to roads.