إيجاد نقطة الوسط في خطّ مستقيم

من موسوعة حسوب
اذهب إلى التنقل اذهب إلى البحث
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

تعثر هذه الخوارزمية على نقطة الوسط لخطّ مستقيم يبدأ من النقطة (x1, y1) وينتهي بالنقطة (x2, y2).

مثال:

Input  : x1 = –1, y1 = 2, 
         x2 = 3, y2 = –6
Output : 1,–2

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

مبدأ عمل الخوارزمية

يمكن إيجاد النقطة التي تقع في منتصف المسافة بين النقطتين (x1, y1) و (x2, y2) باستخدام العلاقة التالية:

M = ((x1+x2)/2 , (y1+y2)/2)
  • C++‎:
#include<iostream> 
using namespace std; 

void midpoint(int x1, int x2, 
				int y1, int y2) 
{ 
	cout << (float)(x1+x2)/2 << 
			" , "<< (float)(y1+y2)/2 ; 
} 

// اختبار الدالة السابقة
int main() 
{ 
	int x1 =-1, y1 = 2 ; 
	int x2 = 3, y2 = -6 ;	 
	midpoint(x1, x2, y1, y2);	 
	return 0; 
}
  • بايثون:
def midpoint(x1, x2, y1, y2): 

	print((x1 + x2) // 2, " , ", 
				(y1 + y2) // 2) 

# اختبار الدالة السابقة 
x1, y1, x2, y2 = -1, 2, 3, -6
midpoint(x1, x2, y1, y2)
  • جافا:
import java.io.*; 

class GFG 
{ 
	static void midpoint(int x1, int x2, 
						int y1, int y2) 
	{ 
		System.out.print((x1 + x2) / 2 + 
						" , " + (y1 + y2) / 2) ; 
	} 

	// اختبار التابع السابق
	public static void main (String[] args) 
	{ 
		int x1 =-1, y1 = 2 ; 
		int x2 = 3, y2 = -6 ; 
		midpoint(x1, x2, y1, y2); 
			
	} 
}

مصادر