LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-08-2022, 06:32 PM   #1
piobair
Member
 
Registered: Aug 2013
Distribution: Debian, Ubuntu
Posts: 271

Rep: Reputation: Disabled
Finding the centroid of a triangle in 3-space


The centroid of a triangle is that point on which a triangle can be simply supported.
The centroid can be found by drawing a line from one corner to the midpoint of the opposite side, and another line from another corner to the midpoint of the opposite side. Where those two lines intersect is the centroid.

Googling "intersection of two lines" gives several examples for the intersection of two lines in 2-space (X and Y coordinates). The following example provides an intersection in 3-space (Z, Y and Z coordinates).

Code:
  double corner[3][3], d, centroid[3]; // x, y, z coordinates of each corner
 
  // C is the vector from corner to midpoint of opposite side
  // the centroid is intersection of C[0] and C[1] (or C2 and 33 or C3 and C4)
  C[0][0] = (corner[1][0] +corner[2][0])/2 -corner[0][0];
  C[0][1] = (corner[1][1] +corner[2][1])/2 -corner[0][1];
  C[0][2] = (corner[1][2] +corner[2][2])/2 -corner[0][2];
  d = sqrt(C[0][0]*C[0][0] +C[0][1]*C[0][1] +C[0][2]*C[0][2]);
  C[0][0] /= d; C[0][1] /= d; C[0][2] /= d; // normalize vector
  C[1][0] = (corner[2][0] +corner[0][0])/2 -corner[1][0];
  C[1][1] = (corner[2][1] +corner[0][1])/2 -corner[1][1];
  C[1][2] = (corner[2][2] +corner[0][2])/2 -corner[1][2];
  d = sqrt(C[1][0]*C[1][0] +C[1][1]*C[1][1] +C[1][2]*C[1][2]);
  C[1][0] /= d; C[1][1] /= d; C[1][2] /= d; // normalize vector
  intersection(corner[0], C[0], corner[1], C[1], centroid);

// find the intersection of two lines thru points p0 & p1, having vectors v0 & v1
void intersection(double *p0, double *v0, double *p1, double *v1, double *intersect){
  double a, b, c, d, v2[3];
	
	// v2 is the vector from p0 to p1
	v2[0] = p1[0] -p0[0];
	v2[1] = p1[1] -p0[1];
	v2[2] = p1[2] -p0[2];
	a = sqrt(v2[0]*v2[0] +v2[1]*v2[1] +v2[2]*v2[2]); // = length (p1 -p0)
	v2[0] /= a; v2[1] /= a; v2[2] /= a;
	// angle between v0 and v1
	b = dotcos(v0, v1);
	b = sqrt(1 -b*b); // sine of angle at p2
	c = dotcos(v0, v2);
	c = sqrt(1 -c*c); // sine of angle at p0
	// d is the distance from p1 to p2
	// from the law of sines, a/b = d/c 
	d = a/b *c; // distance from p1 to p2
	intersect[0] = p1[0] +v1[0]*d;
	intersect[1] = p1[1] +v1[1]*d;
	intersect[2] = p1[2] +v1[2]*d;
}

// return the cosine (dot product of two matrices) of the angle between A & B
double dotcos(double *A, double *B){ 
  double product;
  product = (A[0]*B[0] +A[1]*B[1] +A[2]*B[2]) // this is the dot product
     / (sqrt(A[0]*A[0] + A[1]*A[1] +A[2]*A[2]) *sqrt(B[0]*B[0] + B[1]*B[1] +B[2]*B[2]));
  return(product);
}
 
Old 03-08-2022, 11:20 PM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,880
Blog Entries: 1

Rep: Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871
Code:
for (int i=0; i<3; ++i) {
    centroid[i]= (corner[0][i]+corner[1][i]+corner[2][i])/3.0;
}
 
2 members found this post helpful.
Old 03-09-2022, 07:34 AM   #3
piobair
Member
 
Registered: Aug 2013
Distribution: Debian, Ubuntu
Posts: 271

Original Poster
Rep: Reputation: Disabled
Your solution does provide an identical answer. I am surprised that when I Googled "centroid of a triangle" that solution was not suggested.
Identical answers does provide an indication that my solution for intersection of two lines in 3-space does work.
 
1 members found this post helpful.
Old 03-09-2022, 07:43 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,041

Rep: Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348
https://www.cuemath.com/centroid-formula/
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Please tell how to fix following error while implementing centroid protocol. I am using ns 2.34 on ubuntu14.04lts fozia kosar Linux - Software 12 08-02-2016 03:20 PM
[SOLVED] Triangle alert shows in Notification Area, Fails to Fetch from some repos theAdmiral Debian 6 09-18-2012 09:27 PM
Warning triangle.. herakles_14 Ubuntu 3 12-25-2011 11:30 AM
how to print a triangle in between in java savitrasapre Programming 7 05-19-2009 01:34 AM
triangle notification for new downloads gibney Linux - Newbie 4 06-01-2008 04:51 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:32 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration