Find intersection of two independent ranges and sum values at every coordinate

AEAA

New Member
Joined
Apr 12, 2022
Messages
31
Office Version
  1. 365
  2. 2021
Platform
  1. Windows
Hi All,

I have the definition of several ranges of straight lines: 1, 2, 3, 4 and 5, where I define the x- and the y- coordinates only for the start and end points of the lines. I would like to have a "Desired" range that sums the values in every coordinate of the range. I would like to do this without having to discretize every range to have matching x and y coordinates.

In the chart I provide the desired outcome done by hand, but would like to know if there is a way of doing this in a smarter way using either VBA, PowerQuery or regular excel logic (the latter preferred).

NOTE: If there is an easy way of finding the intersection values when the two lines cross, please let me know. I have found solutions which require user-defined functions in VBA and would prefer avoiding that.

Thank you very much for your help

1649757680679.png


Overlap of plots.xlsx
ABCDEFGHIJKLMNOPQRSTUVWX
1xBottomTop
2-11.15-5.7E-145.68E-14
3-8.61895-287.441280.386
4-7.92687-491.3432454.368
5-6.71974-503.1243804.976
6-5.89574-2779.74025.165
7-4.6473-2972.493236.937
8-3.44744-3141.592454.369
9-1.2251-3141.592170.434
10-0.5-2563.972454.368
111.094949-2487.271286.426
123.85-1926.562189.084
135.165191-1381.532907.325
146.479609-1443.553598.47
157.35-569.9493384.546
168.675-436.2882844.094
1710-302.6282199.361
1810-302.6281524.097
1911.50939-203.1281017.497
201300
21
221Desired (Roughly)
23xyxy
24-11.150-11.150
25-11.151571-11.151571
26-8.618951571-10.05791571
27-8.618950-9.127884025
28-8.618954025
292-8.618942454
30xy-8.219742454
31-10.05790-7.529744025
32-9.127882454-6.719744025
33-8.618952454-6.719740
34-8.618950
35
363
37xy
38-8.618940
39-8.618942454
40-6.719742454
41-6.719740
42
434
44xy
45-8.219740
46-7.52974785
47-6.71974785
48-6.719740
49
505
51xy
52-8.219740
53-7.52974785
54-6.71974785
55-6.719740
Sheet1
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
i saw the following questions here on the site, but i was wondering what are you trying to do, what are you trying to prove ?
You add lines and you want to calculate the intersection and do something with it ?
Can you explain what lines you are adding and why ?
 
Upvote 0
intersection of 2 lines
Map1
ABCD
1x1y1x2y2
2-11,151.571,00-10,060,00
3-8,621.571,00-9,132.454,00
4slope0,002.638,71
5intercept1.571,0026.545,42
6
7xy
8UDF --------->-9,461.571,00
9Formula ---->-9,461.571,00
Blad1
Cell Formulas
RangeFormula
B4,D4B4=SLOPE(B$2:B$3,A$2:A$3)
B5,D5B5=INTERCEPT(B$2:B$3,A$2:A$3)
C8:D8C8=Intersection_2Lines(B4,B5,D4,D5)
C9C9=(D5-B5)/(B4-D4)
D9D9=+C9*B4+B5
Dynamic array formulas.

VBA Code:
Function Intersection_2Lines(Slope1, Intercept1, Slope2, Intercept2)
     x = (Intercept2 - Intercept1) / (Slope1 - Slope2)
     y = Slope1 * x + Intercept1
     Intersection_2Lines = Array(x, y)
End Function
 
Last edited:
Upvote 0
i saw the following questions here on the site, but i was wondering what are you trying to do, what are you trying to prove ?
You add lines and you want to calculate the intersection and do something with it ?
Can you explain what lines you are adding and why ?
Thank you for the reply BSALV.

I have a set of "functions" composed of straight lines with different slopes and I want to add them all up to create my desired red chart. The only way of doing so is to have one x-coordinate range that includes all functions and the y-value of each function at every location. The solution I managed to come up with was to create one stacked column of all x-values and then finding the y-corresponding value by using linear interpolation of each function.

The formula and VBA function that you attached works if the SLOPE and INTERCEPT are part of the same equation, but if I have a piece-wise defined function it does not work.
 
Upvote 0
try me !
What are those red, blue and green lines in #1 ?
Compaired to you data, how do you make them ?

PS. Can you give data with the XL2BB-tool but
X = integer(actual x *100) and Y=integer(actual y)
Normally that has no influence on the result.
(problems with the translation of the comma and the point in the XL2BB-tool, not your fault).
 
Last edited:
Upvote 0
try me !
What are those red, blue and green lines in #1 ?
Compaired to you data, how do you make them ?

So basically the Dark blue, light blue, orange, gray and green lines represent functions 1 through 5. These functions are simply x and y coordinate as you see in A31:B34 for function 2 for example. So I have plotted all these basic lines defined by 3 straight lines with different slopes.

1650029312562.png


And then I have another function for example Function 1:

1650029377866.png


If you overlap these 2 functions you have the Red "Desired" function, which is the sum of these 2:

1650029440973.png

You can see that at coordinate -9.12788 my result is 4025 which is 1571 (Function 1 is straight from -11.15 to -8.61895) + 2454 (Function 2 value at -9.12788).

In order to plot this red lines I need to have therefore all the key points. The interpolation would be useful since I can ask what is the value for Function 1 at x = -9.12788 (undefined) and sum all values at this coordinate.

Does that make sense? Thanks.
 
Upvote 0
See if this does what you want. Given the initial listing in A:B, I would consider using Power Query to transform the data into the table shown in columns D:F (let me know if you need help with that). Then aggregate the x data across all curves and sort it to create a list of x points where at least one of the curves has data (column H). Create table headings for an Initial Helper table that correspond to curve numbers (I24:M24). In this helper table, we examine whether two y-values exist for each x value for each curve. Where two values exist (e.g. a vertical line, so (x,0) and (x,y) pairs are shown), the maximum y value is placed in the helper table. Where two values do not exist for each x value, that represents a known data point, and that y value is also placed in the table. Where we have blanks between y values in the helper table, we know we need to interpolate...that's what the Final Helper table does. It looks forward and backward to determine whether a y value can be found in both directions (meaning that the cell is sandwiched between a pair of known points, and a linear interpolation is performed. This fills in the missing blanks, and sums across the rows of the Final Helper table produce the Results list (column V)...I think this returns what you want, except for possibly the end points, which can be added with separate formulas.
aeaa_20220414.xlsx
ABCDEFGHIJKLMNOPQRSTUV
22Given:PQ to create this listInitial HelperFinal HelperResults
231
24xyxyCurvex12345x12345
25-11.150-11.1501-11.151571    -11.151571    1571
26-11.151571-11.1515711-10.0579 0   -10.057915710   1571
27-8.6189451571-8.61894515711-9.12788 2454   -9.1278815712454   4025
28-8.6189450-8.61894501-8.6189515712454   -8.6189515712454   4025
29-10.0578802-8.61894  2454  -8.61894  2454  2454
302-9.12788124542-8.21974   00-8.21974  2454002454
31xy-8.61894524542-7.52974   785785-7.52974  24547857854024
32-10.057880-8.61894502-6.71974  2454785785-6.71974  24547857854024
33-9.1278812454-8.61894403
34-8.6189452454-8.61894424543
35-8.6189450-6.71973824543
36-6.71973803
373-8.21973804
38xy-7.5297387854
39-8.6189440-6.7197387854
40-8.6189442454-6.71973804
41-6.7197382454-8.21973805
42-6.7197380-7.5297387855
43-6.7197387855
444-6.71973805
45xy
46-8.2197380
47-7.529738785
48-6.719738785
49-6.7197380
50
515
52xy
53-8.2197380
54-7.529738785
55-6.719738785
56-6.7197380
Sheet2
Cell Formulas
RangeFormula
I24:M24,P24:T24I24=TRANSPOSE(SORT(UNIQUE($F$25:$F$44)))
H25:H32,O25:O32H25=SORT(UNIQUE($D$25:$D$44))
I25:M32I25=LET(fwd,INDEX($E$25:$E$44,XMATCH(1,($D$25:$D$44=$H25)*($F$25:$F$44=I$24),0,1)),rev,INDEX($E$25:$E$44,XMATCH(1,($D$25:$D$44=$H25)*($F$25:$F$44=I$24),0,-1)),IF(ISNUMBER(fwd),IF(fwd=rev,fwd,MAX(fwd,rev)),""))
P25:T31P25=LET(fwdx,INDEX($H26:$H$32,XMATCH(1,--(I26:I$32<>""),0,1)),revx,INDEX($H$25:$H25,XMATCH(1,--(I$25:I25<>""),0,-1)),fwdy,INDEX(I26:I$32,XMATCH(1,--(I26:I$32<>""),0,1)),revy,INDEX(I$25:I25,XMATCH(1,--(I$25:I25<>""),0,-1)),m,(revy-fwdy)/(revx-fwdx),b,fwdy-m*fwdx,IF(ISNUMBER(I25),I25,IF(AND(ISNUMBER(fwdy),ISNUMBER(revy)),m*$H26+b,"")))
P32:T32P32=LET(fwdx,INDEX($H$32:$H33,XMATCH(1,--(I$32:I33<>""),0,1)),revx,INDEX($H$25:$H32,XMATCH(1,--(I$25:I32<>""),0,-1)),fwdy,INDEX(I$32:I33,XMATCH(1,--(I$32:I33<>""),0,1)),revy,INDEX(I$25:I32,XMATCH(1,--(I$25:I32<>""),0,-1)),m,(revy-fwdy)/(revx-fwdx),b,fwdy-m*fwdx,IF(ISNUMBER(I32),I32,IF(AND(ISNUMBER(fwdy),ISNUMBER(revy)),m*$H33+b,"")))
V25:V32V25=SUM(P25:T25)
Dynamic array formulas.
 
Upvote 0
Solution
See if this does what you want. Given the initial listing in A:B, I would consider using Power Query to transform the data into the table shown in columns D:F (let me know if you need help with that). Then aggregate the x data across all curves and sort it to create a list of x points where at least one of the curves has data (column H). Create table headings for an Initial Helper table that correspond to curve numbers (I24:M24). In this helper table, we examine whether two y-values exist for each x value for each curve. Where two values exist (e.g. a vertical line, so (x,0) and (x,y) pairs are shown), the maximum y value is placed in the helper table. Where two values do not exist for each x value, that represents a known data point, and that y value is also placed in the table. Where we have blanks between y values in the helper table, we know we need to interpolate...that's what the Final Helper table does. It looks forward and backward to determine whether a y value can be found in both directions (meaning that the cell is sandwiched between a pair of known points, and a linear interpolation is performed. This fills in the missing blanks, and sums across the rows of the Final Helper table produce the Results list (column V)...I think this returns what you want, except for possibly the end points, which can be added with separate formulas.
aeaa_20220414.xlsx
ABCDEFGHIJKLMNOPQRSTUV
22Given:PQ to create this listInitial HelperFinal HelperResults
231
24xyxyCurvex12345x12345
25-11.150-11.1501-11.151571    -11.151571    1571
26-11.151571-11.1515711-10.0579 0   -10.057915710   1571
27-8.6189451571-8.61894515711-9.12788 2454   -9.1278815712454   4025
28-8.6189450-8.61894501-8.6189515712454   -8.6189515712454   4025
29-10.0578802-8.61894  2454  -8.61894  2454  2454
302-9.12788124542-8.21974   00-8.21974  2454002454
31xy-8.61894524542-7.52974   785785-7.52974  24547857854024
32-10.057880-8.61894502-6.71974  2454785785-6.71974  24547857854024
33-9.1278812454-8.61894403
34-8.6189452454-8.61894424543
35-8.6189450-6.71973824543
36-6.71973803
373-8.21973804
38xy-7.5297387854
39-8.6189440-6.7197387854
40-8.6189442454-6.71973804
41-6.7197382454-8.21973805
42-6.7197380-7.5297387855
43-6.7197387855
444-6.71973805
45xy
46-8.2197380
47-7.529738785
48-6.719738785
49-6.7197380
50
515
52xy
53-8.2197380
54-7.529738785
55-6.719738785
56-6.7197380
Sheet2
Cell Formulas
RangeFormula
I24:M24,P24:T24I24=TRANSPOSE(SORT(UNIQUE($F$25:$F$44)))
H25:H32,O25:O32H25=SORT(UNIQUE($D$25:$D$44))
I25:M32I25=LET(fwd,INDEX($E$25:$E$44,XMATCH(1,($D$25:$D$44=$H25)*($F$25:$F$44=I$24),0,1)),rev,INDEX($E$25:$E$44,XMATCH(1,($D$25:$D$44=$H25)*($F$25:$F$44=I$24),0,-1)),IF(ISNUMBER(fwd),IF(fwd=rev,fwd,MAX(fwd,rev)),""))
P25:T31P25=LET(fwdx,INDEX($H26:$H$32,XMATCH(1,--(I26:I$32<>""),0,1)),revx,INDEX($H$25:$H25,XMATCH(1,--(I$25:I25<>""),0,-1)),fwdy,INDEX(I26:I$32,XMATCH(1,--(I26:I$32<>""),0,1)),revy,INDEX(I$25:I25,XMATCH(1,--(I$25:I25<>""),0,-1)),m,(revy-fwdy)/(revx-fwdx),b,fwdy-m*fwdx,IF(ISNUMBER(I25),I25,IF(AND(ISNUMBER(fwdy),ISNUMBER(revy)),m*$H26+b,"")))
P32:T32P32=LET(fwdx,INDEX($H$32:$H33,XMATCH(1,--(I$32:I33<>""),0,1)),revx,INDEX($H$25:$H32,XMATCH(1,--(I$25:I32<>""),0,-1)),fwdy,INDEX(I$32:I33,XMATCH(1,--(I$32:I33<>""),0,1)),revy,INDEX(I$25:I32,XMATCH(1,--(I$25:I32<>""),0,-1)),m,(revy-fwdy)/(revx-fwdx),b,fwdy-m*fwdx,IF(ISNUMBER(I32),I32,IF(AND(ISNUMBER(fwdy),ISNUMBER(revy)),m*$H33+b,"")))
V25:V32V25=SUM(P25:T25)
Dynamic array formulas.
Thanks KRice ! This is sort of what I was producing myself, and it definitely gets the final result I was expecting. Also very little use of PQ which is good as I wanted to keep this simple.
 
Upvote 0
Glad to help. PQ is probably the easiest way to transform your source data into single flat columns. Do your component curves always consist of four points? And is there always a blank row between each group? If there is a consistent pattern, you could probably do the transformation with some formulas. About how many component curves are you dealing with in a typical application?
 
Upvote 0
Here is one idea for transforming your initial set of data into a flat table with formulas, assuming the same structure is maintained throughout:
aeaa_20220414.xlsx
DEF
47xyCurve
48-11.1501
49-11.1515711
50-8.6189515711
51-8.6189501
52-10.057902
53-9.1278824542
54-8.6189524542
55-8.6189502
56-8.6189403
57-8.6189424543
58-6.7197424543
59-6.7197403
60-8.2197404
61-7.529747854
62-6.719747854
63-6.7197404
64-8.2197405
65-7.529747855
66-6.719747855
67-6.7197405
Sheet2
Cell Formulas
RangeFormula
D48:D67D48=FILTER($A$23:$A$56,MOD(SEQUENCE(ROWS($A$23:$A$56)),7)>=3)
E48:E67E48=FILTER($B$23:$B$56,MOD(SEQUENCE(ROWS($A$23:$A$56)),7)>=3)
F48:F67F48=QUOTIENT(ROWS(F$48:F48)-1,4)+1
Dynamic array formulas.
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,193
Members
452,616
Latest member
intern444

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top