
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find the Center of a Circle Using Endpoints of Diameter in C++
Suppose we have two endpoints of diameter of a circle. These are (x1, y1) and (x2, y2), we have to find the center of the circle. So if two points are (-9, 3) and (5, -7), then the center is at location (-2, -2).
We know that the mid points of two points are −
$$(x_{m},y_{m})=\left(\frac{(x_{1}+x_{2})}{2},\frac{(y_{1}+y_{2})}{2}\right)$$
Example
#include<iostream> using namespace std; class point{ public: float x, y; point(float x, float y){ this->x = x; this->y = y; } void display(){ cout << "(" << x << ", " <<y<<")"; } }; point center(point p1, point p2) { int x, y; x = (float)(p1.x + p2.x) / 2; y = (float)(p1.y + p2.y) / 2; point res(x, y); return res; } int main() { point p1(-9.0, 3.0), p2(5.0, -7.0); point res = center(p1, p2); cout << "Center is at: "; res.display(); }
Output
Center is at: (-2, -2)
Advertisements