Skip to content

Instantly share code, notes, and snippets.

@jd-alexander
Last active July 19, 2019 23:10
Show Gist options
  • Save jd-alexander/b343cd8d67b108768d5eedff1bb7fef4 to your computer and use it in GitHub Desktop.
Save jd-alexander/b343cd8d67b108768d5eedff1bb7fef4 to your computer and use it in GitHub Desktop.

Revisions

  1. jd-alexander revised this gist Jul 19, 2019. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion CoordinatesDirectionInteractor.kt
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,8 @@
    I wrote this code recently for a Word Puzzle game I created and this particular class is responsible for processing the touch
    events that occur when the user swipes on different words within the grid. It's suppose to detect the direction that the swipe
    took place in so that it can utilize the direction to determine the coordinates between the start and end event of the touch
    interaction so that it can highlight those letters.
    interaction so that it can highlight those letters. I found this code interesting because I had to use coordinate geometry
    knowledge I learnt in college to solve the issues at hand and I learnt alot about how coordinates are represented on screens.
    **/
    class CoordinatesDirectionInteractor @Inject constructor() {
    private var swipeDirection: SwipeDirection? = null
  2. jd-alexander revised this gist Jul 19, 2019. No changes.
  3. jd-alexander revised this gist Jul 19, 2019. No changes.
  4. jd-alexander revised this gist Jul 19, 2019. No changes.
  5. jd-alexander revised this gist Jul 19, 2019. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions CoordinatesDirectionInteractor.kt
    Original file line number Diff line number Diff line change
    @@ -63,8 +63,8 @@ class CoordinatesDirectionInteractor @Inject constructor() {

    /**
    This function was the most interesting as the diagonals had to determined using formulas from coordinate geometry. Here I
    needed to verify that the line between the two points are at a 45 degree angle to the horizontal (or that the two points would
    be opposite vertices of a square). Below I accomplished this by calculating the difference between the two X and the difference
    needed to verify that the line between the two points are at a 45 degree angle to the horizontal (or that the two points
    would be opposite vertices of a square). Below I accomplished this by calculating the difference between the two X and the difference
    between the two Y values. If those differences are the same then I then attempt to determine the direction the swipe took place in
    and then utilize the compass direction to make it easier to be inferred.
    **/
  6. jd-alexander revised this gist Jul 19, 2019. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions CoordinatesDirectionInteractor.kt
    Original file line number Diff line number Diff line change
    @@ -42,10 +42,10 @@ class CoordinatesDirectionInteractor @Inject constructor() {
    return true
    }

    /*
    /**
    This particular function does the opposite of the above and seeks to verify that the movement is on the y axis which means
    the user swiped vertically so there would be change in the y axis either negatively or positively with the same x axis.
    */
    **/
    private fun areCoordinatesInSameColumn(startCoordinate: WordCoordinate, endCoordinate: WordCoordinate): Boolean {
    if (startCoordinate.x != endCoordinate.x)
    return false
  7. jd-alexander revised this gist Jul 19, 2019. 1 changed file with 20 additions and 1 deletion.
    21 changes: 20 additions & 1 deletion CoordinatesDirectionInteractor.kt
    Original file line number Diff line number Diff line change
    @@ -1,13 +1,17 @@
    /**
    I wrote this code recently for a Word Puzzle game I created and this particular class is responsible for processing the touch
    events that occur when the user swipes on different words within the grid. It's suppose to detect the direction that the swipe
    took place in so that it can utilize the direction with determine the coordinates between the start and end event of the touch
    took place in so that it can utilize the direction to determine the coordinates between the start and end event of the touch
    interaction so that it can highlight those letters.
    **/
    class CoordinatesDirectionInteractor @Inject constructor() {
    private var swipeDirection: SwipeDirection? = null
    private var swipeType: SwipeType? = null

    /**
    This function is the main entry point for the component. It accepts a (x,y) coordinate of the start and end event and returns
    direction (based on the compass) the swipe took place in and the type; whether it is diagonal,vertical or horizontal.
    **/
    fun detectSwipeDirection(startCoordinate: WordCoordinate, endCoordinate: WordCoordinate): Pair<SwipeDirection?, SwipeType?> {

    return when {
    @@ -20,6 +24,10 @@ class CoordinatesDirectionInteractor @Inject constructor() {
    }
    }

    /**
    The logic behind this function is that I checked to see if the start and end coordinate or on the same y axis and if so that means
    they are in the same row so from there the only thing I needed to know was if the movement was negative/positive on the x axis.
    **/
    private fun areCoordinatesInSameRow(startCoordinate: WordCoordinate, endCoordinate: WordCoordinate): Boolean {
    if (startCoordinate.y != endCoordinate.y) return false

    @@ -34,6 +42,10 @@ class CoordinatesDirectionInteractor @Inject constructor() {
    return true
    }

    /*
    This particular function does the opposite of the above and seeks to verify that the movement is on the y axis which means
    the user swiped vertically so there would be change in the y axis either negatively or positively with the same x axis.
    */
    private fun areCoordinatesInSameColumn(startCoordinate: WordCoordinate, endCoordinate: WordCoordinate): Boolean {
    if (startCoordinate.x != endCoordinate.x)
    return false
    @@ -49,6 +61,13 @@ class CoordinatesDirectionInteractor @Inject constructor() {
    return true
    }

    /**
    This function was the most interesting as the diagonals had to determined using formulas from coordinate geometry. Here I
    needed to verify that the line between the two points are at a 45 degree angle to the horizontal (or that the two points would
    be opposite vertices of a square). Below I accomplished this by calculating the difference between the two X and the difference
    between the two Y values. If those differences are the same then I then attempt to determine the direction the swipe took place in
    and then utilize the compass direction to make it easier to be inferred.
    **/
    private fun calculateDiagonalCoordinates(startCoordinate: WordCoordinate, endCoordinate: WordCoordinate): Boolean {

    val diffX = Math.abs(startCoordinate.x - endCoordinate.x)
  8. jd-alexander revised this gist Jul 19, 2019. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions CoordinatesDirectionInteractor.kt
    Original file line number Diff line number Diff line change
    @@ -1,8 +1,8 @@
    /**
    I wrote this code recently for a Word Puzzle game I created and this particular class is responsible for processing the touch events
    that occur when the user swipes on different words within the grid. It's suppose to detect the direction that the swipe took place in
    so that it can utilize the direction with determine the coordinates between the start and end event of the touch interaction so that
    it can highlight those letters.
    I wrote this code recently for a Word Puzzle game I created and this particular class is responsible for processing the touch
    events that occur when the user swipes on different words within the grid. It's suppose to detect the direction that the swipe
    took place in so that it can utilize the direction with determine the coordinates between the start and end event of the touch
    interaction so that it can highlight those letters.
    **/
    class CoordinatesDirectionInteractor @Inject constructor() {
    private var swipeDirection: SwipeDirection? = null
  9. jd-alexander revised this gist Jul 19, 2019. No changes.
  10. jd-alexander revised this gist Jul 19, 2019. 1 changed file with 6 additions and 0 deletions.
    6 changes: 6 additions & 0 deletions CoordinatesDirectionInteractor.kt
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,9 @@
    /**
    I wrote this code recently for a Word Puzzle game I created and this particular class is responsible for processing the touch events
    that occur when the user swipes on different words within the grid. It's suppose to detect the direction that the swipe took place in
    so that it can utilize the direction with determine the coordinates between the start and end event of the touch interaction so that
    it can highlight those letters.
    **/
    class CoordinatesDirectionInteractor @Inject constructor() {
    private var swipeDirection: SwipeDirection? = null
    private var swipeType: SwipeType? = null
  11. jd-alexander revised this gist Jul 19, 2019. No changes.
  12. jd-alexander renamed this gist Jul 19, 2019. 1 changed file with 0 additions and 0 deletions.
  13. jd-alexander created this gist Jul 19, 2019.
    74 changes: 74 additions & 0 deletions CoordinatesDirectionInteractor
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,74 @@
    class CoordinatesDirectionInteractor @Inject constructor() {
    private var swipeDirection: SwipeDirection? = null
    private var swipeType: SwipeType? = null

    fun detectSwipeDirection(startCoordinate: WordCoordinate, endCoordinate: WordCoordinate): Pair<SwipeDirection?, SwipeType?> {

    return when {
    areCoordinatesInSameRow(startCoordinate, endCoordinate) -> Pair(swipeDirection, swipeType)
    areCoordinatesInSameColumn(startCoordinate, endCoordinate) -> Pair(swipeDirection, swipeType)
    else -> {
    calculateDiagonalCoordinates(startCoordinate, endCoordinate)
    Pair(swipeDirection, swipeType)
    }
    }
    }

    private fun areCoordinatesInSameRow(startCoordinate: WordCoordinate, endCoordinate: WordCoordinate): Boolean {
    if (startCoordinate.y != endCoordinate.y) return false

    swipeType = Horizontal

    if (startCoordinate.x < endCoordinate.x) {
    swipeDirection = W.to(E)
    } else if (startCoordinate.x > endCoordinate.x) {
    swipeDirection = E.to(W)
    }

    return true
    }

    private fun areCoordinatesInSameColumn(startCoordinate: WordCoordinate, endCoordinate: WordCoordinate): Boolean {
    if (startCoordinate.x != endCoordinate.x)
    return false

    swipeType = Vertical

    if (startCoordinate.y < endCoordinate.y) {
    swipeDirection = N.to(S)
    } else if (startCoordinate.y > endCoordinate.y) {
    swipeDirection = S.to(N)
    }

    return true
    }

    private fun calculateDiagonalCoordinates(startCoordinate: WordCoordinate, endCoordinate: WordCoordinate): Boolean {

    val diffX = Math.abs(startCoordinate.x - endCoordinate.x)
    val diffY = Math.abs(startCoordinate.y - endCoordinate.y)

    if (diffX == diffY && diffX != 0) {
    swipeType = Diagonal

    if (startCoordinate.x > endCoordinate.x && startCoordinate.y < endCoordinate.y) {
    swipeDirection = NE.to(SW)
    }

    if (startCoordinate.x < endCoordinate.x && startCoordinate.y > endCoordinate.y) {
    swipeDirection = SW.to(NE)
    }

    if (startCoordinate.x < endCoordinate.x && startCoordinate.y < endCoordinate.y) {
    swipeDirection = NW.to(SE)
    }

    if (startCoordinate.x > endCoordinate.x && startCoordinate.y > endCoordinate.y) {
    swipeDirection = SE.to(NW)
    }
    }
    return true
    }

    fun Direction.to(value: Direction): SwipeDirection = SwipeDirection(this, value)
    }