Coder Social home page Coder Social logo

find-coordinate-algorithm's Introduction

find-coordinate-algorithm

Given 3 points with the coordinate (x , y), write an efficient solution in Kotlin to find the fourth point’s coordinate so that 4 points will make a rectangle.

Solutions

  • Step 1: Prove three coordinates are a part of a right triangle
  • Step 2: Find root square
  • Step 3: Find the midpoint hypotenuse
  • Step 4: Find the fourth coordinate

Coding

import kotlin.math.pow

fun main() {
    val points =
        arrayOf(intArrayOf(1, 4), intArrayOf(3, 4), intArrayOf(3, 10)) // Expected > [1, 10]
    println("${solution(points).contentToString()}")
}

fun solution(input: Array<IntArray>): IntArray {
    val a = input[0]
    val b = input[1]
    val c = input[2]
    val ab = (a[0] - b[0]).toDouble().pow(2) + (a[1] - b[1]).toDouble().pow(2)
    val ac = (a[0] - c[0]).toDouble().pow(2) + (a[1] - c[1]).toDouble().pow(2)
    val bc = (b[0] - c[0]).toDouble().pow(2) + (b[1] - c[1]).toDouble().pow(2)
    return when {
        isRightTriangle(ab, ac, bc) -> {
            // bc is hypotenuse -> a is squareRoot
            getFinalPoint(b, c, a)
        }
        isRightTriangle(ab, bc, ac) -> {
            //  ac is hypotenuse -> b is squareRoot
            getFinalPoint(a, c, b)
        }
        else -> {
            //  ab is hypotenuse -> c is squareRoot
            getFinalPoint(a, b, c)
        }
    }
}

fun getFinalPoint(
    firstCoordinates: IntArray,
    secondCoordinates: IntArray,
    squareRoot: IntArray
): IntArray {
    val xMid = (firstCoordinates[0] + secondCoordinates[0]) / 2
    val yMid = (firstCoordinates[1] + secondCoordinates[1]) / 2
    val xFinal = (xMid * 2) - squareRoot[0]
    val yFinal = (yMid * 2) - squareRoot[1]
    return intArrayOf(xFinal, yFinal)
}

fun isRightTriangle(
    firstAngle: Double,
    secondAngle: Double,
    hypotenuse: Double
): Boolean = ((firstAngle + secondAngle) == hypotenuse)

find-coordinate-algorithm's People

Contributors

truongthanhit avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.