多邊形重心計(jì)算
三角形重心
- 頂點(diǎn)為a,b,c的三角形重心為x = (xa + xb + xc) / 3,y = (ya + yb + yc) / 3
多邊形重心
- x = (x1w1 + x2w2 + … + xnwn)/w
- y = (y1w1 + y2w2 + … + ynwn)/w
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
|
import org.locationtech.jts.geom.coordinate; import org.locationtech.jts.geom.polygon; import org.locationtech.jts.io.parseexception; import java.util.*; import java.util.stream.collectors; /** * <p>title : polygonnodetriangle </p> * <p>description : 多邊形自身節(jié)點(diǎn)組成三角形</p> * @author huifer * @date 2018/10/15 */ public class polygonnodetriangle { private int num = 3 ; private set result_p = new hashset(); public static void main(string[] args) { //0 double [] point1 = new double []{ 0 , 0 }; //1 double [] point2 = new double []{ 10 , 0 }; //2 double [] point3 = new double []{ 20 , 0 }; //3 double [] point4 = new double []{ 10 , 10 }; list< double []> allpoint = new arraylist(); allpoint.add(point1); allpoint.add(point3); allpoint.add(point4); polygonnodetriangle polygoncenterpoint = new polygonnodetriangle(); // 外圍 polygon waiwei = polygoncenterpoint.waiwei(point1, point3, point4); // 節(jié)點(diǎn)三角形 list<polygon> sanjiaoxing = polygoncenterpoint.trianglemothed(allpoint); // 外圍內(nèi)所有三角形 list<polygon> rangetriangle = polygoncenterpoint.getrangetriangle(waiwei, sanjiaoxing); // 重心xy double [] gravitycenterxy = polygoncenterpoint.getgravitycenterxy(rangetriangle); system.out.println(rangetriangle.size()); system.out.println( "================================================" ); double [] doubles = polygoncenterpoint. polygongravitypoint( "polygon((0 0, 20 0, 10 10, 0 0))" ); } /*** * polygon wkt 計(jì)算重心 * @param wkt * @return */ private double [] polygongravitypoint(string wkt) { if (!wkt.startswith( "polygon" )) { return null ; } operation operation = new operation(); // 外圍數(shù)據(jù)轉(zhuǎn) list<double[]> polygon waiwei = null ; try { waiwei = operation.createpolygonbywkt(wkt); } catch (parseexception e) { e.printstacktrace(); } coordinate[] coordinates = waiwei.getcoordinates(); list< double []> allp = new arraylist<>(); arrays.stream(coordinates).foreach( s -> { double nowx = s.x; double nowy = s.y; allp.add( new double []{nowx, nowy}); } ); list<polygon> polygons = trianglemothed(allp); list<polygon> rangetriangle1 = getrangetriangle(waiwei, polygons); double area = waiwei.getarea(); double [] gravitycenterxy1 = getgravitycenterxy(rangetriangle1); return gravitycenterxy1; } /*** * 重心值 * @param rangetriangle * @return [x, y] */ private double [] getgravitycenterxy(list<polygon> rangetriangle) { double xarea = 0.0 ; double yarea = 0.0 ; double aarea = 0.0 ; for (polygon triangle : rangetriangle) { coordinate[] coordinates = triangle.getcoordinates(); double area = triangle.getarea(); double [] onegr = trianglecenterofgravity(coordinates[ 0 ], coordinates[ 1 ], coordinates[ 2 ]); xarea += onegr[ 0 ] * area; yarea += onegr[ 1 ] * area; aarea += area; } system.out.println( "重心x " + xarea / aarea); system.out.println( "重心y " + yarea / aarea); return new double []{xarea / aarea, yarea / aarea}; } /*** * 范圍內(nèi)三角形 * @param waiwei * @param sanjiaoxing * @return */ private list<polygon> getrangetriangle(polygon waiwei, list<polygon> sanjiaoxing) { list<polygon> triangle = new arraylist<>(); // 判斷三角形是否在面內(nèi) for ( int i = 0 ; i < sanjiaoxing.size(); i++) { polygon polygon = sanjiaoxing.get(i); boolean within = polygon.within(waiwei); if (within) { triangle.add(polygon); } } return triangle; } /*** * 三角形重心計(jì)算 * @param a * @param b * @param c * @return */ private double [] trianglecenterofgravity(coordinate a, coordinate b, coordinate c) { double gravityx = (a.x + b.x + c.x) / 3 ; double gravityy = (a.y + b.y + c.y) / 3 ; double [] result = new double []{gravityx, gravityy}; return result; } /*** * 測(cè)試用外包圖形 * @return */ private polygon waiwei( double [] point1, double [] point3, double [] point4) { list< double []> ceshimian = new arraylist(); ceshimian.add(point1); // ceshimian.add(point2); // ceshimian.add(point7); ceshimian.add(point4); // ceshimian.add(point6); // ceshimian.add(point5); ceshimian.add(point3); string polygonforlist = createpolygonforlist(ceshimian); operation op = new operation(); polygon polygonbywkt = null ; try { polygonbywkt = op.createpolygonbywkt(polygonforlist); return polygonbywkt; } catch (parseexception e) { e.printstacktrace(); } return null ; } /*** * 生成所有三角形 * @param allpoint * @return */ private list<polygon> trianglemothed(list< double []> allpoint) { // 索引 -> 點(diǎn)坐標(biāo) map<string, double []> indexofpoint = new hashmap(); for ( int i = 0 ; i < allpoint.size(); i++) { indexofpoint.put(string.valueof(i), allpoint.get(i)); } // 排序結(jié)果 sort((list) indexofpoint.keyset().stream().collect(collectors.tolist()), new hashset()); // 刪除元素相同后的集合 // 所有三角形 list<polygon> alltriangle = new arraylist(); for (object onedataobj : result_p) { //這一行數(shù)據(jù) set onedatalist = (set) onedataobj; // 這一行數(shù)據(jù)的三角形數(shù)據(jù) list< double []> trianglepoint = new arraylist(); onedatalist.foreach( s -> trianglepoint.add(indexofpoint.get(s) )); polygon triangle = createtriangle(trianglepoint); if (triangle != null ) { alltriangle.add(triangle); } } // 所有三角形結(jié)束 return alltriangle; } /*** * 從點(diǎn)坐標(biāo)集合中創(chuàng)建一個(gè)面 * @param points * @return */ private static string createpolygonforlist(list< double []> points) { string end = "))" ; string res = "polygon((" ; operation op = new operation(); for ( double [] point : points) { string x = double .tostring(point[ 0 ]); string y = double .tostring(point[ 1 ]); res += x + " " + y + ", " ; } res += double .tostring(points.get( 0 )[ 0 ]) + " " + double .tostring(points.get( 0 )[ 1 ]); res += end; try { op.createpolygonbywkt(res); } catch (parseexception e) { e.printstacktrace(); } return res; } /*** * 創(chuàng)建三角形 * @param trianglepoint * @return polygon */ private static polygon createtriangle(list< double []> trianglepoint) { operation op = new operation(); string trianglewkt; boolean istri = istriangle(trianglepoint); if (istri) { trianglewkt = "polygon((" + trianglepoint.get( 0 )[ 0 ] + " " + trianglepoint.get( 0 )[ 1 ] + ", " + trianglepoint.get( 1 )[ 0 ] + " " + trianglepoint.get( 1 )[ 1 ] + ", " + trianglepoint.get( 2 )[ 0 ] + " " + trianglepoint.get( 2 )[ 1 ] + ", " + trianglepoint.get( 0 )[ 0 ] + " " + trianglepoint.get( 0 )[ 1 ] + "))" ; try { polygon polygonbywkt = op.createpolygonbywkt(trianglewkt); return polygonbywkt; // return trianglewkt; } catch (parseexception e) { e.printstacktrace(); } } return null ; } /*** * 判斷三角形 * @param trianglepoint * @return */ private static boolean istriangle(list< double []> trianglepoint) { double [] doubles = trianglepoint.get( 0 ); double [] doubles1 = trianglepoint.get( 1 ); double [] doubles2 = trianglepoint.get( 2 ); double len = math.sqrt(math.pow(doubles[ 0 ] - doubles1[ 0 ], 2 ) + math.pow(doubles[ 1 ] - doubles1[ 1 ], 2 )); double len1 = math.sqrt(math.pow(doubles[ 0 ] - doubles2[ 0 ], 2 ) + math.pow(doubles[ 1 ] - doubles2[ 1 ], 2 )); double len2 = math.sqrt(math.pow(doubles1[ 0 ] - doubles2[ 0 ], 2 ) + math.pow(doubles1[ 1 ] - doubles2[ 1 ], 2 )); if ((len + len1 > len2) && (len + len2 > len1) && (len1 + len2 > len)) { return true ; } return false ; } /*** * 不重復(fù)排列 (元素不相同) * @param datas * @param target */ private void sort(list datas, set target) { if (target.size() == this .num) { this .result_p.add(target); return ; } for ( int i = 0 ; i < datas.size(); i++) { list newdatas = new arraylist(datas); set newtarget = new hashset(target); newtarget.add(newdatas.get(i)); newdatas.remove(i); sort(newdatas, newtarget); } } } |
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)服務(wù)器之家的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接
原文鏈接:https://blog.csdn.net/staHuri/article/details/83058930