pebble
  • Tutorials
  • Get the SDK
  • Guides
  • Documentation
  • PebbleOS
  • Mobile App
  • Examples
  • Index 01
  • Community
  • Blog
  • More
Privacy
Cookies
Publish

Pebble C API

  • Pebble C API
  • Moddable API (Alloy)
  • PebbleKit JS
  • PebbleKit iOS
  • PebbleKit Android
  • Foundation
    • Alloy
    • App
    • App Communication
    • App Glance
    • AppMessage
    • AppSync
    • AppWorker
    • DataLogging
    • DataStructures
      • UUID
    • Dictation
    • Dictionary
    • Event Service
      • AccelerometerService
      • AlarmService
      • AppFocusService
      • BacklightService
      • BatteryStateService
      • CompassService
      • ConnectionService
      • HealthService
      • TickTimerService
      • TouchService
    • Exit Reason
    • Internationalization
    • Launch Reason
    • Logging
    • Math
    • Memory Management
    • Platform
    • Resources
      • File Formats
    • Storage
    • Timer
    • Wakeup
    • Wall Time
    • WatchInfo
  • Graphics
    • Draw Commands
    • Drawing Paths
    • Drawing Primitives
    • Drawing Text
    • Fonts
    • Graphics Context
    • Graphics Types
      • Color Definitions
  • User Interface
    • Animation
      • PropertyAnimation
    • Clicks
    • Gesture Recognizers
    • Layers
      • ActionBarLayer
      • BitmapLayer
      • MenuLayer
      • RotBitmapLayer
      • ScrollLayer
      • SimpleMenuLayer
      • StatusBarLayer
      • TextLayer
    • Light
    • Preferences
    • Speaker
    • UnobstructedArea
    • Vibes
    • Window
      • ActionMenu
      • NumberWindow
    • Window Stack
  • Worker
  • Standard C
    • Format
    • Locale
    • Math
    • Memory
    • String
    • Time

Math

Math routines.

Below is a code example that uses the trigonometry functions to calculate the coordinate at which the second hand of a watch ends, using seconds from the system time.

GPoint secondHand;
GPoint center;
struct tm *tick_time = ...;
int32_t secondHandLength = ...;
...
int32_t second_angle = TRIG_MAX_ANGLE * tick_time->tm_sec / 60;
secondHand.y = (-cos_lookup(second_angle) * secondHandLength / TRIG_MAX_RATIO) + center.y;
secondHand.x = (sin_lookup(second_angle) * secondHandLength / TRIG_MAX_RATIO) + center.x;

Function Documentation

int32_t sin_lookup(int32_t angle)

Look-up the sine of the given angle from a pre-computed table.

Parameters

angle

The angle for which to compute the cosine. The angle value is scaled linearly, such that a value of 0x10000 corresponds to 360 degrees or 2 PI radians.

int32_t cos_lookup(int32_t angle)

Look-up the cosine of the given angle from a pre-computed table. This is equivalent to calling sin_lookup(angle + TRIG_MAX_ANGLE / 4).

Parameters

angle

The angle for which to compute the cosine. The angle value is scaled linearly, such that a value of 0x10000 corresponds to 360 degrees or 2 PI radians.

int32_t atan2_lookup(int16_t y, int16_t x)

Look-up the arctangent of a given x, y pair The angle value is scaled linearly, such that a value of 0x10000 corresponds to 360 degrees or 2 PI radians.

Macro Definition Documentation

#define TRIG_MAX_RATIO 0xffff

The largest value that can result from a call to sin_lookup or cos_lookup. For a code example, see the detailed description at the top of this chapter: Math.

#define TRIG_MAX_ANGLE 0x10000

Angle value that corresponds to 360 degrees or 2 PI radians.

See Also

sin_lookup
cos_lookup
#define TRIGANGLE_TO_DEG ( trig_angle)

Converts from a fixed point value representation to the equivalent value in degrees.

See Also

DEG_TO_TRIGANGLE
TRIG_MAX_ANGLE
#define DEG_TO_TRIGANGLE ( angle)

Converts from an angle in degrees to the equivalent fixed point value representation.

See Also

TRIGANGLE_TO_DEG
TRIG_MAX_ANGLE

Need some help?

Functions

  • sin_lookup
  • cos_lookup
  • atan2_lookup

Macro Defintions

  • TRIG_MAX_RATIO
  • TRIG_MAX_ANGLE
  • TRIGANGLE_TO_DEG
  • DEG_TO_TRIGANGLE

Getting Help

Do you have questions about the Pebble SDK?

Do you need some help understanding something on this page?

Ask on the developer forum.