Skip to content

Instantly share code, notes, and snippets.

@evilthreads669966
Last active November 23, 2023 16:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save evilthreads669966/1011fc1548998e367fe0d6087b077236 to your computer and use it in GitHub Desktop.
Save evilthreads669966/1011fc1548998e367fe0d6087b077236 to your computer and use it in GitHub Desktop.
draw a line and check if it's a function
import kotlin.math.abs
import kotlin.math.sign
import kotlin.properties.Delegates
fun main() {
println("Do you want to draw a lineor check whether a line is a function?")
loop@while(true){
println("Enter draw or function")
val input = readlnOrNull()?.trim()
if(input.isNullOrBlank()) continue
if(input.lowercase() == "draw"){
var v = false
println("Do you want a sideways V line? No Yes")
var input = readlnOrNull()?.trim()
if(input.isNullOrBlank()) continue@loop
if(input.lowercase() == "yes")
v = true
println("Enter the slope in rise/run format")
input = readlnOrNull()
if(input.isNullOrBlank()) continue
var parts = input.split("/")
val rise = parts[0].toIntOrNull()
if(rise == null) continue
val run = parts[1].toIntOrNull()
if(run == null) continue
val slope = Slope(rise,run)
println("Enter the Y-intercept in x,y format")
input = readlnOrNull()
if(input.isNullOrBlank()) continue
parts = input.split(",")
val x = parts[0].toIntOrNull()
if(x == null) continue
val y = parts[1].toIntOrNull()
if(y == null) continue
val coordinate = Coordinate(x,y)
var line: List<Coordinate> by Delegates.notNull()
if(v)
line = drawVLine(slope,coordinate)
else
line = drawLine(slope, coordinate)
line.forEach {
print("$it ")
}
println()
}
if(input.lowercase() == "function"){
println("Enter a line in (x,y) (x,y) (x,y) format")
val input = readlnOrNull()?.trim()
if(input.isNullOrBlank()) continue
val parts = input.split(" ")
val line = mutableListOf<Coordinate>()
for(part in parts) {
val coordinateParts = part.split(",")
val x = coordinateParts[0].replace("(", "").toIntOrNull()
if(x == null){
println("Bad coordinates")
continue@loop
}
val y = coordinateParts[1].replace(")", "").toIntOrNull()
if(y == null){
println("Bad coordinates")
continue
}
line.add(Coordinate(x,y))
}
val isFunction = isFunction(line)
if(isFunction)
println("Line is function")
else
println("Line is not a function")
}
}
}
fun isFunction(coordinates: List<Coordinate>): Boolean{
val yCoordinates = coordinates.map { it.y }.sorted()
val xCoordinates = coordinates.map { it.x }.sorted()
val highestYCoordinate = yCoordinates.last()
val highestXCoordinate = xCoordinates.last()
val lowestXCoordinate = xCoordinates.first()
val lowestYCoordinate = yCoordinates.first()
for(x in lowestXCoordinate..highestXCoordinate){
var timesIntersected = 0
for(y in lowestYCoordinate..highestYCoordinate){
coordinates.forEach { coordinate ->
if(Coordinate(x,y) == coordinate){
timesIntersected++
}
}
}
if(timesIntersected > 1)
return false
}
return true
}
fun drawLine(slope: Slope, yIntercept: Coordinate): List<Coordinate>{
var currentCoordinate = yIntercept
val coordinates = mutableListOf<Coordinate>()
repeat(5){
val rise = if(slope.rise.sign == -1) abs(slope.rise) else -abs(slope.rise)
val run = if(slope.rise.sign == -1) abs(slope.run) else -abs(slope.run)
val coordinate = Coordinate(currentCoordinate.x + run, currentCoordinate.y + rise )
coordinates.add(coordinate)
currentCoordinate = coordinate
}
coordinates.reverse()
coordinates.add(yIntercept)
currentCoordinate = yIntercept
repeat(5){
val coordinate = Coordinate(currentCoordinate.x + slope.run, currentCoordinate.y + slope.rise )
coordinates.add(coordinate)
currentCoordinate = coordinate
}
return coordinates
}
fun drawVLine(slope: Slope, yIntercept: Coordinate): List<Coordinate>{
var currentCoordinate = yIntercept
var coordinates = mutableListOf<Coordinate>()
repeat(5){
val rise = if(slope.rise.sign == -1) abs(slope.rise) else -abs(slope.rise)
val coordinate = Coordinate(currentCoordinate.x + slope.run, currentCoordinate.y + rise)
coordinates.add(coordinate)
currentCoordinate = coordinate
}
coordinates.reverse()
coordinates.add(yIntercept)
currentCoordinate = yIntercept
repeat(5){
val coordinate = Coordinate(currentCoordinate.x + slope.run, currentCoordinate.y + slope.rise )
coordinates.add(coordinate)
currentCoordinate = coordinate
}
return coordinates
}
class Coordinate(val x: Int, val y: Int){
override fun equals(other: Any?): Boolean {
if(this === other) return true
if(!(other is Coordinate)) return false
if(x == other.x && y == other.y) return true
return false
}
override fun toString(): String {
return "($x,$y)"
}
}
class Slope(val rise: Int, val run: Int)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment