给定三个坐标(x,y),它们是三角形边的中点。任务是找到三角形的坐标。
例子:
输入:midx 1 = 5,midy 1 = 3 midx 2 = 4,midy 2 = 4 midx 3 = 5,midy 3 = 5输出:x 1 = 4 y 1 = 2 x 2 = 4 y 2 = 6 x 3 = 6年3 = 4 解决方案可以通过图中验证。
让我们分别求解X坐标和Y坐标。对于顶点的X坐标,将其设为x 1 ,x 2 ,x 3 。然后,中点的X坐标将为(x 1 + x 2 )/ 2,(x 2 + x 3 )/ 2,(x 3 + x 1 )/ 2。观察到,这三个表达式的总和等于X坐标的总和。现在,我们有3个变量的总和以及每对变量的总和的3个表达式,通过求解方程来找出坐标值。
同样,我们求解Y坐标。
以下是此方法的实现:
C++
// C++ program to find coordinate of the
// triangle given midpoint of each side
#include
#define N 3
using namespace std;
// Return after solving the equations and
// finding the vertices coordinate.
vector solve(int v[])
{
vector res;
// Finding sum of all three coordinate.
int all3 = v[0] + v[1] + v[2];
// Solving the equation.
res.push_back(all3 - v[1]*2);
res.push_back(all3 - v[2]*2);
res.push_back(all3 - v[0]*2);
return res;
}
// Finds vertices of a triangles from given
// middle vertices.
void findVertex(int xmid[], int ymid[])
{
// Find X coordinates of verices.
vector V1 = solve(xmid);
// Find Y coordinates of verices.
vector V2 = solve(ymid);
// Output the solution.
for (int i = 0; i < 3; i++)
cout << V1[i] << " "
<< V2[i] <
Java
import java.util.Vector;
// Java program to find coordinate of the
// triangle given midpoint of each side
class GFG {
//static final int N = 3;
// Return after solving the equations and
// finding the vertices coordinate.
static Vector solve(int v[]) {
Vector res = new Vector();
// Finding sum of all three coordinate.
int all3 = v[0] + v[1] + v[2];
// Solving the equation.
res.add(all3 - v[1] * 2);
res.add(all3 - v[2] * 2);
res.add(all3 - v[0] * 2);
return res;
}
// Finds vertices of a triangles from given
// middle vertices.
static void findVertex(int xmid[], int ymid[]) {
// Find X coordinates of verices.
Vector V1 = solve(xmid);
// Find Y coordinates of verices.
Vector V2 = solve(ymid);
// Output the solution.
for (int i = 0; i < 3; i++) {
System.out.println(V1.get(i) + " " + V2.get(i));
}
}
// Driver code
public static void main(String[] args) {
int xmid[] = {5, 4, 5};
int ymid[] = {3, 4, 5};
findVertex(xmid, ymid);
}
}
// This code is contributed by
// PrinciRaj1992
Python3
# Python3 program to find coordinate of the
# triangle given midpoint of each side
N = 3
# Return after solving the equations and
# finding the vertices coordinate.
def solve(v):
res = []
# Finding sum of all three coordinate.
all3 = v[0] + v[1] + v[2]
# Solving the equation.
res.append(all3 - v[1] * 2)
res.append(all3 - v[2] * 2)
res.append(all3 - v[0] * 2)
return res
# Finds vertices of a triangles from given
# middle vertices.
def findVertex(xmid, ymid):
# Find X coordinates of verices.
V1 = solve(xmid)
# Find Y coordinates of verices.
V2 = solve(ymid)
# Output the solution.
for i in range(0, 3):
print(V1[i], end=" ")
print(V2[i])
# Driver code
if __name__=='__main__':
xmid = [5, 4, 5]
ymid = [3, 4, 5]
findVertex(xmid, ymid)
# This code is contributed by
# Sanjit_Prasad
C#
// C# program to find coordinate of the
// triangle given midpoint of each side
using System;
using System.Collections;
class GFG
{
//static final int N = 3;
// Return after solving the equations and
// finding the vertices coordinate.
static ArrayList solve(int []v)
{
ArrayList res = new ArrayList();
// Finding sum of all three coordinate.
int all3 = v[0] + v[1] + v[2];
// Solving the equation.
res.Add(all3 - v[1] * 2);
res.Add(all3 - v[2] * 2);
res.Add(all3 - v[0] * 2);
return res;
}
// Finds vertices of a triangles from given
// middle vertices.
static void findVertex(int []xmid, int []ymid)
{
// Find X coordinates of verices.
ArrayList V1 = solve(xmid);
// Find Y coordinates of verices.
ArrayList V2 = solve(ymid);
// Output the solution.
for (int i = 0; i < 3; i++)
{
Console.WriteLine(V1[i] + " " + V2[i]);
}
}
// Driver code
public static void Main()
{
int []xmid = {5, 4, 5};
int []ymid = {3, 4, 5};
findVertex(xmid, ymid);
}
}
// This code is contributed by mits
PHP
输出:
6 4
4 2
4 6
时间复杂度: O(1)