Problem Statement
You are given two distinct points A and B in the two-dimensional plane. Your task is to find any point C with the following properties:
C is different from A and B.
Each coordinate of C is an integer between -100 and 100, inclusive.
The distance between A and C is strictly greater than the distance between B and C.
You are given four ints: x1, y1, x2, and y2. Point A has coordinates (x1,y1) and point B has coordinates (x2,y2). Find the coordinates (x3,y3) of one possible point C with the above properties. Return these coordinates as a vector with two elements: element 0 is x3 and element 1 is y3. In other words, return the vector {x3,y3}.
For the constraints given below it is guaranteed that a valid point C always exists. If there are multiple solutions, return any of them.
Definition
Class:
PointDistance
Method:
findPoint
Parameters:
int, int, int, int
Returns:
vector
Method signature:
vector findPoint(int x1, int y1, int x2, int y2)
(be sure your method is public)
Limits
Time limit (s):
2.000
Memory limit (MB):
256
Stack limit (MB):
256
Notes
In this problem we consider the standard Euclidean distance. Formally, the distance between points (xi,yi) and (xj,yj) is defined as sqrt( (xi-xj)^2 + (yi-yj)^2 ).
Constraints
x1,y1,x2,y2 will be between -50 and 50, inclusive.
(x1,y1) will be different from (x2,y2).
Examples
0)
-1
0
1
0
Returns: {8, 48 }
In this example, point A is at (-1,0) and point B is at (1,0). Almost any point with a positive x-coordinate will be a valid answer. For example, your program can also return {100,100}, {2,0}, or {9,-100}. Note that you cannot return {1,0} because point C must not be the same as point B.
1)
1
1
-1
-1
Returns: {25, -63 }
(x1,y1) is (1,1) and (x2,y2) is (-1,-1).
2)
0
1
2
3
Returns: {41, 65 }
3)
5
-4
-2
5
Returns: {68, 70 }
4)
-50
-50
50
-50
Returns: {67, 4 }
5)
-50
50
-49
49
Returns: {73, -25 }
This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.
My Solution
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;
class PointDistance
{
public:
vector <int> findPoint(int x1, int y1, int x2, int y2);
};
vector<int> PointDistance::findPoint(int x1, int y1, int x2, int y2)
{
vector <int> ans(2);
ans[0] = 0.25 * (x1 - x2) + x2;
ans[1] = 0.25 * (y1 - y2) + y2;
return ans;
}