Search code examples
javaandroidcomputational-geometry

How do I properly determine if user is dragging my circle wheel slider Clockwise or Counter Clockwise using time?


I have a custom view that is shaped like a clock. I have a slider image that a user can use to slide around the clock counter clockwise (CCW) or clockwise (CW) so they can select time. My custom view returns me a time of data type String in a H:mm aa format. e.g.: 1:34 AM.

I'm currently able to determine a CW or CCW detection but it is not full proof as there is an issue when users slide past the 12:00 AM point.

My current logic for determining CW or CCW movement is:

  1. Convert time into military time so I can store it as a Date object known as mDate.
  2. Check if the last timeToMil is less than the current mDate.getTime(). If it is less, I assume it's CW, else it's CCW.
  3. Store the current number of milliseconds since January 1, 1970 into variable timeToMil so the next iteration can determine if the last point moved forward in time or moved backwards.

The issue though is if the users last time for example is 11:00 PM and moves the slider to 1:00AM. Going with #2 logic, this will detect it to be CCW which is incorrect.

if (timeToMil < mDate.getTime()) {
    cw = true; 
} else {
    cw = false;
}

I was wondering if there's a better approach than this? I've been looking at it for way too long that I don't think it's productive anymore and it would help to see some outside perspectives.

I was thinking about converting all the minutes into some sort of radians but I'm not sure if that effort is the way to go.

EDIT: Adding Photo:

enter image description here

The Gray circle is the image that a user can drag along the red line either clockwise or counter clockwise. Sliding left is CCW, sliding it right is CW


Solution

  • h - hours [1,12] m - minutes [0,59]

    720 = 12*60 - minutes for full circle of hour hand

    halfOfCircle = 360 = 720/2 - minutes for half-circle of hour hand

    timePrev = timeNext
    timeNext = (h%12)*60+m
    
    if (abs(timePrev-timeNext)<halfOfCircle){
        if (timePrev<timeNext){
            return "CW"
        } else {
            return "CCW"
        }
    } else {
        if (timeNext<halfOfCircle){
            return "CW"
        } else {
            return "CCW"
        }
    }