Skip to content

Commit

Permalink
暂时提交一下。还没改完。
Browse files Browse the repository at this point in the history
  • Loading branch information
ice1000 committed Aug 8, 2016
1 parent ba8d806 commit 850da34
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 24 deletions.
52 changes: 46 additions & 6 deletions src/core/Graph.kt
Original file line number Diff line number Diff line change
@@ -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

/**
Expand All @@ -13,33 +17,69 @@ import javax.imageio.ImageIO
class Graph(file: File) {
val image: BufferedImage
val cache: BufferedImage
val mark: Array<Array<Boolean>>

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<Line>()
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)

}
17 changes: 7 additions & 10 deletions src/core/models/Line.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
9 changes: 5 additions & 4 deletions src/core/models/Point.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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()

}
14 changes: 14 additions & 0 deletions src/utils/exceptions/LineNotFoundException.kt
Original file line number Diff line number Diff line change
@@ -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))

}
7 changes: 7 additions & 0 deletions src/utils/package-info.java
Original file line number Diff line number Diff line change
@@ -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;
8 changes: 4 additions & 4 deletions test/core/models/Point.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ package core.models
*/

fun main(args: Array<String>) {
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)
}

0 comments on commit 850da34

Please sign in to comment.