diff --git a/src/core/Graph.kt b/src/core/Graph.kt index 121f77d..7e99428 100644 --- a/src/core/Graph.kt +++ b/src/core/Graph.kt @@ -1,9 +1,13 @@ package core +import core.models.Line +import core.models.Point import core.processors.BinaryProcessor +import utils.exceptions.LineNotFoundException import java.awt.Color import java.awt.image.BufferedImage import java.io.File +import java.util.* import javax.imageio.ImageIO /** @@ -13,33 +17,69 @@ import javax.imageio.ImageIO class Graph(file: File) { val image: BufferedImage val cache: BufferedImage + val mark: Array> init { image = ImageIO.read(file) cache = ImageIO.read(file) + mark = Array(cache.height, { + Array(cache.width, { false }) + }) init() } fun init() { (0..image.width - 1).forEach { x -> (0..image.height - 1).forEach { y -> - image.setRGB( - x, y, - getBin(x, y) - ) + image.setRGB(x, y, color(x, y)) + mark[x][y] = this[x, y] } } } + /** + * please handle an exception + */ + fun findLine(x: Int, y: Int) { + var line = ArrayList() + if (!mark[x][y]) throw LineNotFoundException(x, y) +// deprecated +// var max = Max(x, y, -1.0) +// (-1..1).forEach { x -> +// (-1..1).forEach { y -> +// if (x == 0 && y == 0) return +// var p = Point(x, y) +// +// } +// } + } + + fun findLine(point: Point) = findLine(point.x, point.y) + /** * @param x x in image * @param y y in image * @return black or white */ - private fun getBin(x: Int, y: Int) = - if (BinaryProcessor.getGray(cache.getRGB(x, y)) > average) + private fun color(x: Int, y: Int) = + if (get(x, y)) Color.WHITE.rgb else Color.BLACK.rgb + private fun color(point: Point) = + color(point.x, point.y) + + operator fun get(x: Int, y: Int) = + BinaryProcessor.getGray(cache.getRGB(x, y)) > average + + operator fun get(point: Point) = this[point.x, point.y] + + private fun isLine(x: Point, y: Point): Boolean { + // tOdO + return true + } + + data class Max(val x: Int, val y: Int, val length: Double) + } \ No newline at end of file diff --git a/src/core/models/Line.kt b/src/core/models/Line.kt index 21f75eb..3a37393 100644 --- a/src/core/models/Line.kt +++ b/src/core/models/Line.kt @@ -8,17 +8,14 @@ import core.deviation */ class Line(val a: Double, val b: Double, val c: Double) { - /** - * is the point located on (x, y) belong to this line - */ - operator fun get(x: Double, y: Double) = on(x, y) + /** is the point located on (x, y) belong to this line */ + operator fun get(x: Int, y: Int) = on(x, y) - operator fun get(point: Point) = - get(point.x, point.y) + operator fun get(point: Point) = get(point.x, point.y) - fun on(x: Double, y: Double) = - Math.abs(bring(x, y)) < deviation + /** 判断一个点在不在方程上 */ + fun on(x: Int, y: Int) = Math.abs(bring(x, y)) < deviation - fun bring(x: Double, y: Double) = - a * x + b * y + c + /** 将一个点带入直线方程 */ + fun bring(x: Int, y: Int) = a * x + b * y + c } \ No newline at end of file diff --git a/src/core/models/Point.kt b/src/core/models/Point.kt index e80b070..4cc5545 100644 --- a/src/core/models/Point.kt +++ b/src/core/models/Point.kt @@ -4,17 +4,18 @@ package core.models * @author ice1000 * Created by ice1000 on 2016/8/8. */ -class Point(val x: Double, val y: Double) { +class Point(val x: Int, val y: Int) { var quadrant = 0 - /** - * 计算象限 - */ + /** 计算象限 */ init { quadrant += if (x >= 0) 1 else 2 quadrant += if (y >= 0) 0 else 3 if (quadrant == 5) quadrant = 3 } + override fun toString(): String = + x.toString() + y.toString() + } diff --git a/src/utils/exceptions/LineNotFoundException.kt b/src/utils/exceptions/LineNotFoundException.kt new file mode 100644 index 0000000..62841c5 --- /dev/null +++ b/src/utils/exceptions/LineNotFoundException.kt @@ -0,0 +1,14 @@ +package utils.exceptions + +import core.models.Point + +/** + * @author ice1000 + * Created by ice1000 on 2016/8/8. + */ +class LineNotFoundException(val point: Point) : + Exception("no line found at point" + point.toString()) { + + constructor(x: Int, y: Int) : this(Point(x, y)) + +} \ No newline at end of file diff --git a/src/utils/package-info.java b/src/utils/package-info.java new file mode 100644 index 0000000..fd33596 --- /dev/null +++ b/src/utils/package-info.java @@ -0,0 +1,7 @@ +/** + * @author ice1000 + * Created by ice1000 on 2016/8/8. + * + * sth which I have no idea where 2 put them. + */ +package utils; \ No newline at end of file diff --git a/test/core/models/Point.kt b/test/core/models/Point.kt index 2cb46b3..99b314f 100644 --- a/test/core/models/Point.kt +++ b/test/core/models/Point.kt @@ -6,8 +6,8 @@ package core.models */ fun main(args: Array) { - println(Point(1.0, 1.0).quadrant) - println(Point(-1.0, 1.0).quadrant) - println(Point(-1.0, -1.0).quadrant) - println(Point(1.0, -1.0).quadrant) + println(Point(1, 1).quadrant) + println(Point(-1, 1).quadrant) + println(Point(-1, -1).quadrant) + println(Point(1, -1).quadrant) } \ No newline at end of file