找到直线中点的程序

给定直线的两个坐标,起点为(x1,y1),终点为(x2,y2),找出直线的中点。 例如:

null
Input  : x1 = –1, y1 = 2,          x2 = 3, y2 = –6Output : 1,–2Input  : x1 = 6.4, y1 = 3          x2 = –10.7, y2 = 4Output : –2.15, 3.5

图片[1]-找到直线中点的程序-yiteyi-C++库

中点公式: 两点(x1,y2)和(x2,y2)的中点是通过以下公式找到的点M: M=((x) 1. +x 2. )/2、(y) 1. +y 2. )/2)

C++

// C++ program to find
// the midpoint of a line
#include<iostream>
using namespace std;
// function to find the
// midpoint of a line
void midpoint( int x1, int x2,
int y1, int y2)
{
cout << ( float )(x1+x2)/2 <<
" , " << ( float )(y1+y2)/2 ;
}
// Driver Function to test above
int main()
{
int x1 =-1, y1 = 2  ;
int x2 = 3, y2 = -6 ;
midpoint(x1, x2, y1, y2);
return 0;
}


JAVA

// Java program to find
// the midpoint of a line
import java.io.*;
class GFG
{
// function to find the
// midpoint of a line
static void midpoint( int x1, int x2,
int y1, int y2)
{
System.out.print((x1 + x2) / 2 +
" , " + (y1 + y2) / 2 ) ;
}
// Driver code
public static void main (String[] args)
{
int x1 =- 1 , y1 = 2 ;
int x2 = 3 , y2 = - 6 ;
midpoint(x1, x2, y1, y2);
}
}
// This code is contributed by vt_m.


Python3

# Python3 program to find
# the midpoint of a line
# Function to find the
# midpoint of a line
def midpoint(x1, x2, y1, y2):
print ((x1 + x2) / / 2 , " , " ,
(y1 + y2) / / 2 )
# Driver Code
x1, y1, x2, y2 = - 1 , 2 , 3 , - 6
midpoint(x1, x2, y1, y2)
# This code is contributed by Anant Agarwal.


C#

// C# program to find
// the midpoint of a line
using System;
class GFG
{
// function to find the
// midpoint of a line
static void midpoint( int x1, int x2,
int y1, int y2)
{
Console.WriteLine((x1 + x2) / 2 +
" , " + (y1 + y2) / 2) ;
}
// Driver code
public static void Main ()
{
int x1 =-1, y1 = 2 ;
int x2 = 3, y2 = -6 ;
midpoint(x1, x2, y1, y2);
}
}
// This code is contributed by vt_m.


PHP

<?php
// PHP program to find
// the midpoint of a line
// function to find the
// midpoint of a line
function midpoint( $x1 , $x2 , $y1 , $y2 )
{
echo ((float)( $x1 + $x2 )/2 . " , " .
(float)( $y1 + $y2 )/2) ;
}
// Driver Code
$x1 = -1; $y1 = 2 ;
$x2 = 3; $y2 = -6 ;
midpoint( $x1 , $x2 , $y1 , $y2 );
// This code is contributed by Ajit.
?>


Javascript

<script>
// JavaScript program to find
// the midpoint of a line
// function to find the
// midpoint of a line
function midpoint(x1, x2,
y1, y2)
{
document.write((x1 + x2) / 2 +
" , " + (y1 + y2) / 2) ;
}
// Driver code
let x1 =-1, y1 = 2 ;
let x2 = 3, y2 = -6 ;
midpoint(x1, x2, y1, y2);
</script>


输出:

1 , -2

© 版权声明
THE END
喜欢就支持一下吧,技术咨询可以联系QQ407933975
点赞15 分享