Skip to content

Commit 850da34

Browse files
committed
暂时提交一下。还没改完。
1 parent ba8d806 commit 850da34

6 files changed

Lines changed: 83 additions & 24 deletions

File tree

src/core/Graph.kt

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
package core
22

3+
import core.models.Line
4+
import core.models.Point
35
import core.processors.BinaryProcessor
6+
import utils.exceptions.LineNotFoundException
47
import java.awt.Color
58
import java.awt.image.BufferedImage
69
import java.io.File
10+
import java.util.*
711
import javax.imageio.ImageIO
812

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

1722
init {
1823
image = ImageIO.read(file)
1924
cache = ImageIO.read(file)
25+
mark = Array(cache.height, {
26+
Array(cache.width, { false })
27+
})
2028
init()
2129
}
2230

2331
fun init() {
2432
(0..image.width - 1).forEach { x ->
2533
(0..image.height - 1).forEach { y ->
26-
image.setRGB(
27-
x, y,
28-
getBin(x, y)
29-
)
34+
image.setRGB(x, y, color(x, y))
35+
mark[x][y] = this[x, y]
3036
}
3137
}
3238
}
3339

40+
/**
41+
* please handle an exception
42+
*/
43+
fun findLine(x: Int, y: Int) {
44+
var line = ArrayList<Line>()
45+
if (!mark[x][y]) throw LineNotFoundException(x, y)
46+
// deprecated
47+
// var max = Max(x, y, -1.0)
48+
// (-1..1).forEach { x ->
49+
// (-1..1).forEach { y ->
50+
// if (x == 0 && y == 0) return
51+
// var p = Point(x, y)
52+
//
53+
// }
54+
// }
55+
}
56+
57+
fun findLine(point: Point) = findLine(point.x, point.y)
58+
3459
/**
3560
* @param x x in image
3661
* @param y y in image
3762
* @return black or white
3863
*/
39-
private fun getBin(x: Int, y: Int) =
40-
if (BinaryProcessor.getGray(cache.getRGB(x, y)) > average)
64+
private fun color(x: Int, y: Int) =
65+
if (get(x, y))
4166
Color.WHITE.rgb
4267
else
4368
Color.BLACK.rgb
4469

70+
private fun color(point: Point) =
71+
color(point.x, point.y)
72+
73+
operator fun get(x: Int, y: Int) =
74+
BinaryProcessor.getGray(cache.getRGB(x, y)) > average
75+
76+
operator fun get(point: Point) = this[point.x, point.y]
77+
78+
private fun isLine(x: Point, y: Point): Boolean {
79+
// tOdO
80+
return true
81+
}
82+
83+
data class Max(val x: Int, val y: Int, val length: Double)
84+
4585
}

src/core/models/Line.kt

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,14 @@ import core.deviation
88
*/
99
class Line(val a: Double, val b: Double, val c: Double) {
1010

11-
/**
12-
* is the point located on (x, y) belong to this line
13-
*/
14-
operator fun get(x: Double, y: Double) = on(x, y)
11+
/** is the point located on (x, y) belong to this line */
12+
operator fun get(x: Int, y: Int) = on(x, y)
1513

16-
operator fun get(point: Point) =
17-
get(point.x, point.y)
14+
operator fun get(point: Point) = get(point.x, point.y)
1815

19-
fun on(x: Double, y: Double) =
20-
Math.abs(bring(x, y)) < deviation
16+
/** 判断一个点在不在方程上 */
17+
fun on(x: Int, y: Int) = Math.abs(bring(x, y)) < deviation
2118

22-
fun bring(x: Double, y: Double) =
23-
a * x + b * y + c
19+
/** 将一个点带入直线方程 */
20+
fun bring(x: Int, y: Int) = a * x + b * y + c
2421
}

src/core/models/Point.kt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,18 @@ package core.models
44
* @author ice1000
55
* Created by ice1000 on 2016/8/8.
66
*/
7-
class Point(val x: Double, val y: Double) {
7+
class Point(val x: Int, val y: Int) {
88

99
var quadrant = 0
1010

11-
/**
12-
* 计算象限
13-
*/
11+
/** 计算象限 */
1412
init {
1513
quadrant += if (x >= 0) 1 else 2
1614
quadrant += if (y >= 0) 0 else 3
1715
if (quadrant == 5) quadrant = 3
1816
}
1917

18+
override fun toString(): String =
19+
x.toString() + y.toString()
20+
2021
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package utils.exceptions
2+
3+
import core.models.Point
4+
5+
/**
6+
* @author ice1000
7+
* Created by ice1000 on 2016/8/8.
8+
*/
9+
class LineNotFoundException(val point: Point) :
10+
Exception("no line found at point" + point.toString()) {
11+
12+
constructor(x: Int, y: Int) : this(Point(x, y))
13+
14+
}

src/utils/package-info.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/**
2+
* @author ice1000
3+
* Created by ice1000 on 2016/8/8.
4+
*
5+
* sth which I have no idea where 2 put them.
6+
*/
7+
package utils;

test/core/models/Point.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ package core.models
66
*/
77

88
fun main(args: Array<String>) {
9-
println(Point(1.0, 1.0).quadrant)
10-
println(Point(-1.0, 1.0).quadrant)
11-
println(Point(-1.0, -1.0).quadrant)
12-
println(Point(1.0, -1.0).quadrant)
9+
println(Point(1, 1).quadrant)
10+
println(Point(-1, 1).quadrant)
11+
println(Point(-1, -1).quadrant)
12+
println(Point(1, -1).quadrant)
1313
}

0 commit comments

Comments
 (0)