Repository navigation
Expand file tree
/
Copy pathContour.java
More file actions
executable file
·75 lines (70 loc) · 1.76 KB
/
Copy pathContour.java
File metadata and controls
executable file
·75 lines (70 loc) · 1.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import java.util.List;
import java.awt.Point;
import java.util.ArrayList;
public class Contour{
public static List<Point> getContour(Matrix mat){
List<Point> contour=new ArrayList<Point>();
//scan every row and column
//boundary
if(mat.getRow()==1 || mat.getCol()==1)
onlyOne(mat,contour);
else if(mat.getRow()>1 && mat.getCol()>1)
hasMuch(mat,contour);
return contour;
}
private static void onlyOne(Matrix mat,List<Point> list){
for(int i=0;i<mat.getRow();++i){
for(int j=0;j<mat.getCol();++j){
if(mat.getElem(i,j)!=0)
list.add(new Point(i,j));
}
}
}
private static void hasMuch(Matrix mat,List<Point> contour){
for(int i=0;i<mat.getRow();++i){
//internal
for(int j=0;j<mat.getCol();++j){
if(mat.getElem(i,j)==0)
continue;
if(j==0){
if(mat.getElem(i,j+1)==0){
contour.add(new Point(i,j));
}
else if(i==0){
if( mat.getElem(i+1,j)==0)
contour.add(new Point(i,j));
}
else if(i==mat.getRow()-1){
if(mat.getElem(i-1,j)==0)
contour.add(new Point(i,j));
}
}
else if(j==mat.getCol()-1){
if(mat.getElem(i,j-1)==0){
contour.add(new Point(i,j));
}
else if(i==0){
if(mat.getElem(i+1,j)==0)
contour.add(new Point(i,j));
}
else if(i==mat.getRow()-1){
if(mat.getElem(i-1,j)==0)
contour.add(new Point(i,j));
}
}
else if(mat.getElem(i,j-1)==0 || mat.getElem(i,j+1)==0)
contour.add(new Point(i,j));
else if(i==0){
if(mat.getElem(i+1,j)==0)
contour.add(new Point(i,j));
}
else if(i==mat.getRow()-1){
if(mat.getElem(i-1,j)==0)
contour.add(new Point(i,j));
}
else if(mat.getElem(i-1,j)==0 || mat.getElem(i+1,j)==0)
contour.add(new Point(i,j));
}
}
}
}