Author Topic: Math problem revisited.  (Read 3296 times)

0 Members and 1 Guest are viewing this topic.

Offline NOVARSEG

  • Forum Resident
  • Posts: 509
    • View Profile
Math problem revisited.
« on: June 20, 2021, 10:26:16 pm »
https://www.qb64.org/forum/index.php?topic=2714.30

A  triangle (call it the base triangle) is formed by 3 points x, y, z   Find if a 4th  point x, y ,z rests on the triangle or not.

I find  @Ashish solution interesting.  However the whole thread (I think) is dealing with 2D and I thought Ashish's solution would work for 3D as well.

Consider a 4th point not on the triangle.  There are 4 points total x, y, z which form a tetrahedron.    4  triangles form the tetrahedron.  If the 4th point is not on the base triangle then the total area of the 3 remaining triangles will be greater than the area of the base triangle.

Need some math to prove that.   It looks like the 2D case was proven already so it should work for 3D .

« Last Edit: June 20, 2021, 10:52:31 pm by NOVARSEG »

Offline NOVARSEG

  • Forum Resident
  • Posts: 509
    • View Profile
Re: Math problem revisited.
« Reply #1 on: June 21, 2021, 12:42:27 am »
Some code

In the code example, the 4th point is not on the triangle.?

Code: QB64: [Select]
  1. TYPE V3
  2.     X AS SINGLE
  3.     Y AS SINGLE
  4.     Z AS SINGLE
  5.  
  6. DIM P1 AS V3
  7. DIM P2 AS V3
  8. DIM P3 AS V3
  9. DIM P4 AS V3
  10.  
  11. P1.X = 10: P1.Y = 2: P1.Z = 1 'base triangle
  12. P2.X = 9: P2.Y = 6: P2.Z = 7 'base triangle
  13. P3.X = 3: P3.Y = 4: P3.Z = 3 'base triangle
  14. P4.X = 1: P4.Y = 3: P4.Z = 2 'point to test
  15.  
  16. DISTANCE P1, P2, d1 'side lengths of base triangle
  17. DISTANCE P1, P3, d2 'side lengths of base triangle
  18. DISTANCE P2, P3, d3 'side lengths of base triangle
  19.  
  20. DISTANCE P1, P4, d4 'other side lengths of tetrahedron
  21. DISTANCE P2, P4, d5 'other side lengths of tetrahedron
  22. DISTANCE P3, P4, d6 'other side lengths of tetrahedron
  23.  
  24.  
  25. AREA d1, d2, d3, a1 'base triangle
  26. AREA d1, d4, d5, a2 'other
  27. AREA d2, d6, d4, a3 'other
  28. AREA d3, d5, d6, a4 'other
  29.  
  30.  
  31. PRINT a2 + a3 + a4
  32.  
  33.  
  34.  
  35. 'Heron's formula for area of a triangle
  36. SUB AREA (var1, var2, var3, var4 AS SINGLE)
  37.     ta = var1 + var2 + var3
  38.     var4 = (ta + (ta - var1) + (ta - var2) + (ta - var3)) ^ .5
  39.  
  40.  
  41. SUB DISTANCE (Var1 AS V3, var2 AS V3, var3 AS SINGLE)
  42.     var3 = ((Var1.X - var2.X) ^ 2 + (Var1.Y - var2.Y) ^ 2 + (Var1.Z - var2.Z) ^ 2) ^ .5
the dotty stuff is OK

I can't get the 4th point on the triangle.  Testing with a 3,4,5 right triangle to see where the math went wrong.

I'm getting the correct area for a 3,4,5 triangle but the other areas are not correct.
« Last Edit: June 21, 2021, 01:29:22 am by NOVARSEG »

Offline MasterGy

  • Seasoned Forum Regular
  • Posts: 327
  • people lie, math never lies
    • View Profile
Re: Math problem revisited.
« Reply #2 on: June 21, 2021, 01:49:39 am »
Hello ! i just looked at the area calculation and there may be 2 things wrong. I’m not sure, but where I found the heron formula, I rewrote it that way. See if that works.

Code: QB64: [Select]
  1. SUB AREA (var1, var2, var3, var4 AS SINGLE)
  2.     ta = (var1 + var2 + var3) / 2
  3.     var4 = (ta * ((ta - var1) + (ta - var2) + (ta - var3))) ^ .5
  4.  

Offline NOVARSEG

  • Forum Resident
  • Posts: 509
    • View Profile
Re: Math problem revisited.
« Reply #3 on: June 21, 2021, 03:00:47 am »
@MasterGy

 Yes that was where the error was. Good catch.

 I watched a video on Heron's formula and got it totally wrong.  Here is updated code it seems to work now.

It shows a 3 ,4, 5 triangle with z = 0  and the print out shows equal areas, so point  4 in this case is on the base triangle. 

I'm going to put some iterations in the next program so if, say, x and y are known for point 4 then it will calculate z etc.

Code: QB64: [Select]
  1. TYPE V3
  2.     X AS SINGLE
  3.     Y AS SINGLE
  4.     Z AS SINGLE
  5.  
  6. DIM P1 AS V3
  7. DIM P2 AS V3
  8. DIM P3 AS V3
  9. DIM P4 AS V3
  10.  
  11. P1.X = 0: P1.Y = 0: P1.Z = 0 'base triangle
  12. P2.X = 0: P2.Y = 4: P2.Z = 0 'base triangle
  13. P3.X = 3: P3.Y = 0: P3.Z = 0 'base triangle
  14. P4.X = 1: P4.Y = 1: P4.Z = 0 'point to test
  15.  
  16. DISTANCE P1, P2, d1 'side lengths of base triangle
  17. DISTANCE P1, P3, d2 'side lengths of base triangle
  18. DISTANCE P2, P3, d3 'side lengths of base triangle
  19.  
  20. DISTANCE P1, P4, d4 'other side lengths of tetrahedron
  21. DISTANCE P2, P4, d5 'other side lengths of tetrahedron
  22. DISTANCE P3, P4, d6 'other side lengths of tetrahedron
  23.  
  24. AREA d1, d2, d3, a1 'base triangle
  25. AREA d1, d4, d5, a2 'other
  26. AREA d2, d4, d6, a3 'other
  27. AREA d3, d5, d6, a4 'other
  28.  
  29. PRINT a2 + a3 + a4
  30.  
  31. 'Heron's formula for area of a triangle
  32. SUB AREA (var1, var2, var3, var4 AS SINGLE)
  33.     ta = (var1 + var2 + var3) / 2
  34.     var4 = (ta * (ta - var1) * (ta - var2) * (ta - var3)) ^ .5
  35.  
  36. SUB DISTANCE (Var1 AS V3, var2 AS V3, var3 AS SINGLE)
  37.     var3 = ((Var1.X - var2.X) ^ 2 + (Var1.Y - var2.Y) ^ 2 + (Var1.Z - var2.Z) ^ 2) ^ .5
  38.  

« Last Edit: June 26, 2021, 07:15:34 pm by NOVARSEG »

Offline Ashish

  • Forum Resident
  • Posts: 630
  • Never Give Up!
    • View Profile
Re: Math problem revisited.
« Reply #4 on: June 26, 2021, 08:37:29 am »
Hmmmmm...... I will post something here in few days.
if (Me.success) {Me.improve()} else {Me.tryAgain()}


My Projects - https://github.com/AshishKingdom?tab=repositories
OpenGL tutorials - https://ashishkingdom.github.io/OpenGL-Tutorials