I’ve been really busy with various things associated with my new house lately and not really had much time to tinker and meddle with Mathematica or my PhD. One of the key elements that I need to investigate is to calculate the internal area of a polygon where cables cross… This will vary, depending on how many internal cables that I have and so I need a quick way of finding out the internal area for lots of different permutations.

This is a simple polygon problem which can be easily calculated once you know the cartesian co-ordinates for each of the key points using the following equation which is presented for the general condition. It’s not a hard calculation, as really it’s just triangles, but it is a tedious calculation, which means it’s perfect for Mathematica or Excel.

\[Area = \left| {\frac{{\left( {{x_1}{y_2} – {y_1}{x_2}} \right) + \left( {{x_2}{y_3} – {y_2}{x_3}} \right) \ldots + \left( {{x_n}{y_1} – {y_n}{x_1}} \right)}}{2}} \right|\]

However, the tricky part comes from determining where the cables actually cross over as that is where the cartesian co-ordinates will come from that are needed to determine the areas. The calculation of the co-ordinates can be done a few different ways, but I’ve decided to solve the problem through using some vector based geometry, consider the simple condition below which is just for two lines that cross, how do we find the point where the lines intersect?

You could describe the two lines in the general term of y=mx+c, but this isn’t what I want to do within Mathematica as I want to automate this for a wide range of values and lines and so iterative geometry using vectors is likely to be far more beneficial. Representing these two lines in vector format using cartesian co-ordinates gives:

\[\begin{array}{l} Lin{e_1} = \left( {1 – \lambda } \right){P_1} + \lambda {P_2}\\ Lin{e_2} = \left( {1 – \mu } \right){P_3} + \mu {P_4} \end{array}\]

Now we know the equations in vector format it becomes possible to solve both of these equations to determine λ and μ which when back substituted into either equation will give us the co-ordinate for the intersection point p5 in (x,y) format.

This isn’t that difficult to do by hand, but requires a bit of vector manipulation to expand the brackets and given my low attention span I would inevitably get it wrong after a few run throughs, but in Mathematica the problem gets much more simple, so for the example below we’ll assume that the four co-ordinates are at 4m away from the origin (0,0) that is point p1.

**{p1, p2, p3, p4} := {{0, 0}, {4, 4}, {0, 4}, {4, 0}};**

**Solve[{(1 – λ) p1 + λ*p2 == (1 – μ) p3 + μ*p4}, {λ, μ}, Reals];**

**p5 = Flatten[(1 – λ) p1 + λ*p2 /. %]**

Executing this codes gives the intersection as being at point (2,2) in a fraction of a second, which is where you would expect it to be. Now to explain the three lines of code and what they do, I’m sure a more experienced user could make this code much neater but doing it this way makes sense to my tiny brain.

The first line simply sets the variables p1-p4 with their corresponding cartesian co-ordinate position, giving the point two in the top right corner 4m up and 4m across, p3 is along the bottom 4m away, p4 is straight up in the top left corner 4m above point p1, as shown in the figure below.

The second line then solves the two vector equations of the lines for λ and μ and makes sure that they are real numbers, rather than imaginary. The real part probably isn’t necessary, but it’s just a force of habit that I put this in when using the solver as a just incase.

The final line substitutes the λ value back into the equation and then stores this as variable p5 so that I can use this in up and coming calculations, this was the fiddly part for me and makes use of the strange /. % input that allows the equation to effectively go back and substitute the solutions from the Solve command into the equation specified and recalculate and because my equation has the λ within it Mathematica knows this is what I want substituting in even without me being explicit. I spent ages trying to work out this final stage, but now I’ve cracked it as a principle I can start to roll it out on the other sheets that I’ve been working on and this was the reason for writing this post incase it too helps someone else. Now I can directly write the solution outputs from Solve, FindRoot, and NSolve and then carry on using these within my calculations rather than having to manually type them in.

This process lets me know that the intersection point is at (2,2) and now I know that the process works I can start to extrapolate this algebraically for the general conditions and start to calculate the areas of my polygons.

All of the figures in this post have been written using OmniGraffle, which I’m slowly getting to grips with, I can’t make it dance as well as I can Visio, but I think it will help create some nice looking diagrams for my thesis once I get more proficient with it.