Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
118 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
/* | ||
@title: City at Night - Snowflakes Edition | ||
@author: alexdevz | ||
@snapshot: snowflake-city | ||
*/ | ||
|
||
const buildingCount = 7; | ||
const minBuildingWidth = 10; | ||
const maxBuildingWidth = 20; | ||
const minBuildingHeight = 20; | ||
const maxBuildingHeight = 100; | ||
const buildingColors = ['gray', 'darkgray', 'lightgray']; | ||
const windowColor = 'yellow'; | ||
const windowSize = 2; | ||
const moonRadius = 10; | ||
const moonColor = 'white'; | ||
const snowflakeCount = 100; | ||
const snowflakeColor = 'white'; | ||
const snowflakeSize = 0.5; | ||
const skyColor = 'black'; | ||
|
||
setDocDimensions(125, 125); | ||
|
||
function randomInRange(min, max) { | ||
return Math.random() * (max - min) + min; | ||
} | ||
|
||
function randomColor() { | ||
const index = Math.floor(Math.random() * buildingColors.length); | ||
return buildingColors[index]; | ||
} | ||
|
||
function sky() { | ||
const bottomLeft = [0, 0]; | ||
const topLeft = [0, 125]; | ||
const topRight = [125, 125]; | ||
const bottomRight = [125, 0]; | ||
return [bottomLeft, topLeft, topRight, bottomRight, bottomLeft]; | ||
} | ||
|
||
const skyPoints = sky(); | ||
drawLines([skyPoints], { fill: skyColor }); | ||
|
||
//buildings and windows | ||
function building(x, width, height) { | ||
const bottomLeft = [x, 0]; | ||
const bottomRight = [x + width, 0]; | ||
const topLeft = [x, height]; | ||
const topRight = [x + width, height]; | ||
return [bottomLeft, topLeft, topRight, bottomRight, bottomLeft]; | ||
} | ||
|
||
function windows(x, width, height) { | ||
const windows = []; | ||
for (let i = x + windowSize; i < x + width; i += windowSize * 2) { | ||
for (let j = windowSize; j < height; j += windowSize * 2) { | ||
const window = [ | ||
[i, j], | ||
[i + windowSize, j], | ||
[i + windowSize, j + windowSize], | ||
[i, j + windowSize], | ||
[i, j] | ||
]; | ||
windows.push(window); | ||
} | ||
} | ||
return windows; | ||
} | ||
//moon | ||
function moon() { | ||
const moon = []; | ||
const centerX = moonRadius; | ||
const centerY = 125 - moonRadius; | ||
for (let i = 0; i <= 360; i += 5) { | ||
const angle = i * Math.PI / 180; | ||
const x = centerX + moonRadius * Math.cos(angle); | ||
const y = centerY + moonRadius * Math.sin(angle); | ||
moon.push([x, y]); | ||
} | ||
return moon; | ||
} | ||
|
||
drawLines([moon()], { fill: moonColor }); | ||
|
||
//building | ||
let x = 0; | ||
for (let i = 0; i < buildingCount; i++) { | ||
const width = randomInRange(minBuildingWidth, maxBuildingWidth); | ||
const height = randomInRange(minBuildingHeight, maxBuildingHeight); | ||
const buildingPoints = building(x, width, height); | ||
drawLines([buildingPoints], { fill: randomColor() }); | ||
const windowPoints = windows(x, width, height); | ||
drawLines(windowPoints, { fill: windowColor }); | ||
x += width; | ||
} | ||
|
||
//snow | ||
function snowflake(x, y) { | ||
const topLeft = [x, y]; | ||
const topRight = [x + snowflakeSize, y]; | ||
const bottomRight = [x + snowflakeSize, y + snowflakeSize]; | ||
const bottomLeft = [x, y + snowflakeSize]; | ||
return [topLeft, topRight, bottomRight, bottomLeft, topLeft]; | ||
} | ||
|
||
for (let i = 0; i < snowflakeCount; i++) { | ||
let x = randomInRange(0, 125); | ||
let y = randomInRange(0, 125); | ||
|
||
// avoid moon | ||
while (Math.sqrt(Math.pow(x - moonRadius, 2) + Math.pow(y - (125 - moonRadius), 2)) < moonRadius) { | ||
x = randomInRange(0, 125); | ||
y = randomInRange(0, 125); | ||
} | ||
|
||
const snowflakePoints = snowflake(x, y); | ||
drawLines([snowflakePoints], { fill: snowflakeColor }); | ||
} |
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.