Problem Statement
A description of the problem can be found on Hackerrank.
Solution
For solution you can check wikipedia page for Point reflection.
I created solution in:
All solutions are also available on my GitHub.
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
import java.util.*; public class FindPoint { public static void main(String[] args) { Scanner stdin = new Scanner(System.in); int points = stdin.nextInt(); for(int i = 0; i < points; i++) { int x = stdin.nextInt(); int y = stdin.nextInt(); int mx = stdin.nextInt(); int my = stdin.nextInt(); int sx = 2 * mx - x; int sy = 2 * my - y; System.out.println(sx + " " + sy); } stdin.close(); } } |
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
function processData(input) { var lines = input.split("\n"); //var points = parseInt(lines[0]); for(var i = 1; i < lines.length; i++) { var coordinates = lines[i].split(" ").map(i => parseInt(i)); var x = coordinates[0]; var y = coordinates[1]; var mx = coordinates[2]; var my = coordinates[3]; var sx = 2 * mx - x; var sy = 2 * my - y; console.log(sx + " " + sy); } } process.stdin.resume(); process.stdin.setEncoding("ascii"); _input = ""; process.stdin.on("data", function (input) { _input += input; }); process.stdin.on("end", function () { processData(_input); }); |
Scala
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import scala.io.Source case class Point(val x: Int, val y: Int) { def reflection(midPoint: Point): Point = { val rx = 2 * midPoint.x - x val ry = 2 * midPoint.y - y Point(rx, ry) } } object FindPoint extends App { val coordinates = Source.stdin.getLines().drop(1) coordinates.foreach { lines => { val ints = lines.split(" ").map(_.toInt) val point = Point(ints(0), ints(1)) val midPoint = Point(ints(2), ints(3)) val symmetricPoint = point.reflection(midPoint) println(s"${symmetricPoint.x} ${symmetricPoint.y}") } } } |
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
class Point attr_reader :x, :y def initialize(x, y) @x = x @y = y end def self.symetric_point(point, midpoint) x = 2 * midpoint.x - point.x y = 2 * midpoint.y - point.y return Point.new(x, y) end end tests = gets.chomp.to_i tests.times do coordinates = gets.chomp.split.map { |e| e.to_i } half1 = coordinates.slice(0..1) half2 = coordinates.slice(2..3) point = Point.new(half1[0], half1[1]) midpoint = Point.new(half2[0], half2[1]) symetric_point = Point.symetric_point(point, midpoint) puts "#{symetric_point.x} #{symetric_point.y}" end |