Created
June 15, 2020 22:14
-
-
Save holtbp/2f586b19efd1befcc61568706b97ede4 to your computer and use it in GitHub Desktop.
ASCII scatterplot from coordinates
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
coords = [ | |
Coordinate.new(x=0.0, y=1.8), | |
Coordinate.new(x=1.5, y=2.2), | |
Coordinate.new(x=2.8, y=3.5), | |
Coordinate.new(x=5.0, y=4.2), | |
Coordinate.new(x=2.0, y=4.2), | |
] | |
class Coordinate | |
attr_reader :x, :y | |
def initialize(in_x, in_y) | |
@x = in_x | |
@y = in_y | |
end | |
end | |
def scatterplot_coordinates(coordinates, length_x = 20, length_y = 20) | |
max_value_x = coordinates.max_by(&:x).x | |
max_value_y = coordinates.max_by(&:y).y | |
increment_x = max_value_x / (length_x - 1) | |
increment_y = max_value_y / (length_y) | |
graph = Array.new(length_y) { Array.new(length_x, '.') } | |
coordinates.each do |coordinate| | |
x_index = (coordinate.x / increment_x).round() | |
y_index = length_y - (coordinate.y / increment_y).round() | |
graph[y_index][x_index] = 'X' | |
end | |
graph.each do |row| | |
puts row.join(' ') | |
end | |
end | |
scatterplot_coordinates(coords) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment