# Find the intersection points between two data sets

14 ビュー (過去 30 日間)
Mohamad Alkhalidi 2020 年 7 月 6 日
コメント済み: Mohamad Alkhalidi 2020 年 7 月 6 日
If I have two data sets(each from xy data) and they are intersect at more than two points, How can I find the area between intersection points? If there are more than one area, i.e., Area1, Area2, etc, I would like to find each area separately and assign +ve and -ve sign to the areas? Figure and Example of my data is below (data1 and data2):
data1 = [575721.678739338 3189196.15577354
575711.702452018 3189195.13447944
575701.688569224 3189195.60208872
575691.697320475 3189195.17336187
575681.678990798 3189195.81713592
575676.797287407 3189195.96672958
575671.667154953 3189196.20376653
575661.619557374 3189198.00667871
575651.499830521 3189202.66611234
575645.227659483 3189205.17163764
575641.392720288 3189206.82592006
575637.150456055 3189214.96975033
575633.985427757 3189224.89189997
575630.732860735 3189232.87583130
575629.825200565 3189234.78891918
575626.268606140 3189244.70118035
575625.055756129 3189254.67262378
575624.048800203 3189264.64926620
575626.033070719 3189274.70144150
575628.301047757 3189284.76078116
575629.348139014 3189287.71323321
575630.865563282 3189294.82760927
575632.585061618 3189304.87309963
575633.749353391 3189314.90457031
575634.569486755 3189324.92735054
575636.649511930 3189334.98194565
575638.050011320 3189339.20256643
575640.817786629 3189345.08927361
575647.656595518 3189354.86415159
575647.870775185 3189355.26944727
575654.747228098 3189365.44516408
575657.269399466 3189370.27939153
575662.074406641 3189375.63226375
575667.058357271 3189378.71883487
575674.871973717 3189385.95750447
575676.800091128 3189389.02837035
575684.133094564 3189396.19344337
575686.586685119 3189397.56143505
575696.422894795 3189404.12972127
575700.864844918 3189406.61803541
575706.247405814 3189411.16129194
575715.751567506 3189416.99603830
575716.098781051 3189417.12905527
575726.036514414 3189419.67706070
575736.017098070 3189420.52822388
575745.982395609 3189421.98474492
575756.005774219 3189421.14128728
575766.053610679 3189419.32932771
575769.136073507 3189418.34413225
575776.223506655 3189412.68383576
575779.240944960 3189408.59722472
575786.447078405 3189403.91279079
575796.523744120 3189400.95926314
575799.389111391 3189399.10393297
575805.535189846 3189389.25705318
575806.854089989 3189387.95995545
575816.972618517 3189383.34868686
575819.537289559 3189379.60855471
575822.008081040 3189369.66886365
575827.547621183 3189360.66081291
575828.761131424 3189359.83730830
575836.931299811 3189350.04153712
575837.844535516 3189348.98535223
575840.943246114 3189340.14076154
575843.602853864 3189330.20583632
575846.984577107 3189320.28914546
575848.756275143 3189312.96214604
575850.522840530 3189310.37640707
575854.053180112 3189300.46346810
575854.964811253 3189290.48440209
575851.181415037 3189280.38677937
575849.764141289 3189273.04921042
575849.517599011 3189270.34267971
575843.653125281 3189260.19250792
575840.207154651 3189255.42251920
575835.594666340 3189249.98693602
575830.470162708 3189244.92434067
575823.627599270 3189239.68266813
575820.649830226 3189237.72661781
575810.781232117 3189232.44033033
575805.255617650 3189229.21667049
575800.963379230 3189225.14444292
575791.086532261 3189220.18486820
575787.656625601 3189218.77019437
575781.182921444 3189216.28522513
575771.266091576 3189212.90911181
575761.354718952 3189209.31690449
575758.250526886 3189208.02557942
575751.503530051 3189203.34130216
575741.563262126 3189200.89346790
575731.630100857 3189198.16421964
575727.023285731 3189197.23498323
575721.678739338 3189196.15577354]
data2 = [575731.422838196 3189206.37232435
575724.697961952 3189207.17834314
575721.394010027 3189207.43174664
575711.364654134 3189208.51209622
575701.337970259 3189209.48665581
575691.319597425 3189210.13210704
575681.265867243 3189212.17781946
575671.229649118 3189213.53004162
575661.238791658 3189213.08590592
575651.213949200 3189213.98768238
575647.113409301 3189215.22132567
575640.991573607 3189222.71223818
575639.486204725 3189225.03080093
575630.773146661 3189231.28042522
575626.361206512 3189234.70144909
575620.515832815 3189241.38861678
575619.227201771 3189244.52337575
575615.294804439 3189254.42614638
575611.728754337 3189264.33816728
575610.825894415 3189274.31743727
575610.019497925 3189284.29914302
575612.199764593 3189294.35626689
575614.140766277 3189304.40734924
575616.755327358 3189314.47544050
575618.552527076 3189319.13850901
575621.878551158 3189324.60688061
575628.162248551 3189334.67588513
575628.217729738 3189334.76902672
575635.418719721 3189344.95293626
575637.762350507 3189350.59416235
575640.873491159 3189355.09275120
575647.522419377 3189360.17761406
575652.554788934 3189365.38980024
575657.207443596 3189372.73287452
575660.346312788 3189375.58862551
575667.041781573 3189379.37523935
575673.376126510 3189385.91973087
575676.723934510 3189392.04419395
575680.359244947 3189396.09814467
575686.526280383 3189399.95347416
575693.260598684 3189406.42600951
575696.245920828 3189411.13790683
575701.943322200 3189416.64734516
575706.050815019 3189418.94628958
575715.930434190 3189423.79558285
575723.484025899 3189427.19337957
575725.793998076 3189429.28068546
575735.775124065 3189430.11037054
575745.755309984 3189430.97731069
575750.945388100 3189427.88685048
575755.879236329 3189426.15217806
575765.956578764 3189423.17178706
575776.025683486 3189420.51763495
575779.517405427 3189418.60628718
575785.884931838 3189408.76500175
575786.332681882 3189408.44290783
575796.433874881 3189404.51810025
575806.560091648 3189399.60237019
575808.088283081 3189399.32360785
575816.676357286 3189395.08072864
575819.981180111 3189389.62184792
575826.994371634 3189382.56978788
575832.212012345 3189379.92861994
575837.242777695 3189372.81535473
575838.008516413 3189370.07290944
575840.812110902 3189360.14162087
575842.594128381 3189350.18453504
575847.474455532 3189340.30568737
575848.111345114 3189338.50205485
575854.967611222 3189330.49281788
575855.989375110 3189320.51653295
575857.810750377 3189310.56043949
575854.671003314 3189300.47906917
575852.328243476 3189290.41782454
575849.406644524 3189287.20664394
575845.929355550 3189280.25415686
575841.477934729 3189270.13966651
575839.935268430 3189266.18969967
575834.930938952 3189259.97226068
575830.172676589 3189256.70535602
575823.102617400 3189249.67149545
575820.455440295 3189245.42484822
575813.145830725 3189239.41799054
575810.633609741 3189238.28648202
575800.759767900 3189233.20788699
575791.729334709 3189228.87511614
575790.885959615 3189228.12798485
575781.089451899 3189219.98683027
575777.562658171 3189218.51531071
575771.160574863 3189217.08781557
575761.222464867 3189214.55447377
575751.293622469 3189211.65414062
575741.345269581 3189209.52649525
575734.009662553 3189207.41347302
575731.422838196 3189206.37232435]

### 採用された回答

John D'Errico 2020 年 7 月 6 日

Finding the intersection point is easy enough.
[intx,inty] = intersections(data1(:,1),data1(:,2),data2(:,1),data2(:,2))
intx =
575631.634509305
575854.22624686
inty =
3189230.66259473
3189298.56901274
I used the utility intersections, written by Doug Schwarz. It resides on the file exchange for download.
Now, one approach could be to convert to polar coordinates, around the center of the region. View each as a curve r1(theta) and r2(theta).
xyc = mean([data1;data2])
xyc =
575734.849631717 3189307.42475141
[th1,r1] = cart2pol(data1(:,1) - xyc(1),data1(:,2) - xyc(2));
[th2,r2] = cart2pol(data2(:,1) - xyc(1),data2(:,2) - xyc(2));
Now you have a pair of functions, r(theta). You can convert the intersection points to find the polar angle of intersection around that central point.
atan2(inty - xyc(2),intx - xyc(1))
ans =
-2.50212907679585
-0.0740475617728026
atan2d(inty - xyc(2),intx - xyc(1))
ans =
-143.361435897367
-4.24261277281584
Now, I might choose to integrate the function:
(r1(theta) - r2(theta)*r(theta)
between those limits. Do NOT forget to put that r(theta) term in there - calculus can be a hard taskmaster if you ignore it. You could use integral to do the work, interpolating each of r1 and r2 using interp1. Or you could use trapz. Sorry, but I won't do it all for you. It should work easily enough. The sign of one of these areas will be positive, the other negative.
There is another approach you can take, that in the end, might be fairly nice to use. Use polyshapes.
ps1 = polyshape(data1(:,1),data1(:,2));
ps2 = polyshape(data2(:,1),data2(:,2));
plot(ps1)
hold on
plot(ps2)
hold off
But it appears you want to find the regions where the two curves do not overlap. xor gives you that. As you can see here, there are two such regions.
pxor = xor(ps1,ps2)
pxor =
polyshape with properties:
Vertices: [191×2 double]
NumRegions: 2
NumHoles: 0
Now, extract each region into a separate polyshape, and then compute the area.
psub = regions(pxor)
psub =
2×1 polyshape array with properties:
Vertices
NumRegions
NumHoles
area(psub)
ans =
2325.03206760974
3385.39300601318
Compared to the area of the overall region, those numbers make sense, with each under 10% of the total area.
area(intersect(ps1,ps2))
ans =
36781.1693640184
area(union(ps1,ps2))
ans =
42491.5944376413
figure
plot(psub)
You should recognize how easily polyshapes solve the problem. Just a few simple lines of code, and I already had the solution. While the first solution I outlined was not difficult, the solution using polyshapes is by far the simpler. A little extra work would allow you to figure out the signs on those areas. For example, I might test to see which of the two curves lies farther away from the overall centroid in that area. That would imply the sign on the area.
But simpler? Again, use polyshapes.
all(isinterior(ps1,psub(1).Vertices))
ans =
logical
1
all(isinterior(ps1,psub(2).Vertices))
ans =
logical
0
So all of the vertices of the xored sub-region in psub(1) are inside ps1. But psub(2) lies outside of ps1. And this tells the the two signs. As expected, this is exactly the opposite for ps2.
all(isinterior(ps2,psub(1).Vertices))
ans =
logical
0
all(isinterior(ps2,psub(2).Vertices))
ans =
logical
1
So we can now directly infer the two areas must have opposite signs.

#### 1 件のコメント

Mohamad Alkhalidi 2020 年 7 月 6 日
Wow. Thank you very much. Is it possible to get help in the integration using the "trapz". I'm not expert in MATLAB.

R2019b

Translated by