15 #include <Ultralight/Defines.h> 28 struct {
float x, y; };
33 inline vec2(
float x,
float y) : x(x), y(y) {}
35 inline vec2(
float x) : x(x), y(x) {}
37 inline vec2 yx()
const {
return { y, x }; }
39 inline vec2 xx()
const {
return { x, x }; }
41 inline vec2 yy()
const {
return { y, y }; }
43 inline friend vec2 operator+(
vec2 lhs,
const vec2& rhs) { lhs += rhs;
return lhs; }
45 inline friend vec2 operator-(
vec2 lhs,
const vec2& rhs) { lhs -= rhs;
return lhs; }
47 inline friend vec2 operator*(
vec2 lhs,
const vec2& rhs) { lhs *= rhs;
return lhs; }
49 inline friend vec2 operator/(
vec2 lhs,
const vec2& rhs) { lhs /= rhs;
return lhs; }
51 inline friend vec2 operator+(
vec2 lhs,
float rhs) { lhs += rhs;
return lhs; }
53 inline friend vec2 operator-(
vec2 lhs,
float rhs) { lhs -= rhs;
return lhs; }
55 inline friend vec2 operator*(
vec2 lhs,
float rhs) { lhs *= rhs;
return lhs; }
57 inline friend vec2 operator/(
vec2 lhs,
float rhs) { lhs /= rhs;
return lhs; }
59 inline vec2& operator+=(
const vec2& rhs) {
60 value[0] += rhs.value[0];
61 value[1] += rhs.value[1];
65 inline vec2& operator-=(
const vec2& rhs) {
66 value[0] -= rhs.value[0];
67 value[1] -= rhs.value[1];
71 inline vec2& operator*=(
const vec2& rhs) {
72 value[0] *= rhs.value[0];
73 value[1] *= rhs.value[1];
77 inline vec2& operator/=(
const vec2& rhs) {
78 value[0] /= rhs.value[0];
79 value[1] /= rhs.value[1];
83 inline vec2& operator+=(
float rhs) {
89 inline vec2& operator-=(
float rhs) {
95 inline vec2& operator*=(
float rhs) {
101 inline vec2& operator/=(
float rhs) {
107 inline friend bool operator==(
const vec2& a,
const vec2& b) {
108 return !memcmp(&a, &b,
sizeof(a));
111 inline friend bool operator!=(
const vec2& a,
const vec2& b) {
116 return{ (b.x < a.x) ? b.x : a.x,
117 (b.y < a.y) ? b.y : a.y };
121 return{ (a.x < b.x) ? b.x : a.x,
122 (a.y < b.y) ? b.y : a.y };
125 inline friend vec2 clamp(
const vec2& x,
const vec2& minVal,
const vec2& maxVal) {
126 return min_(max_(x, minVal), maxVal);
129 inline friend vec2 mix(
const vec2& a,
const vec2& b,
float t) {
130 return a * (1.0f - t) + b * t;
133 inline friend float length(
const vec2& a) {
134 return sqrtf(a.x * a.x + a.y * a.y);
138 inline friend float length2(
const vec2& a) {
142 inline friend float distance(
const vec2& a,
const vec2& b) {
143 return length(a - b);
147 inline friend float distance2(
const vec2& a,
const vec2& b) {
148 return length2(a - b);
151 inline friend vec2 normalize(
const vec2& a) {
152 return a / length(a);
155 inline friend float dot(
const vec2& a,
const vec2& b) {
156 return a.x * b.x + a.y * b.y;
166 struct {
float x, y, z; };
171 inline vec3(
float x,
float y,
float z) : x(x), y(y), z(z) {}
173 inline vec3(
float x) : x(x), y(x), z(x) {}
175 inline friend vec3 operator+(
vec3 lhs,
const vec3& rhs) { lhs += rhs;
return lhs; }
177 inline friend vec3 operator-(
vec3 lhs,
const vec3& rhs) { lhs -= rhs;
return lhs; }
179 inline friend vec3 operator*(
vec3 lhs,
const vec3& rhs) { lhs *= rhs;
return lhs; }
181 inline friend vec3 operator/(
vec3 lhs,
const vec3& rhs) { lhs /= rhs;
return lhs; }
183 inline friend vec3 operator+(
vec3 lhs,
float rhs) { lhs += rhs;
return lhs; }
185 inline friend vec3 operator-(
vec3 lhs,
float rhs) { lhs -= rhs;
return lhs; }
187 inline friend vec3 operator*(
vec3 lhs,
float rhs) { lhs *= rhs;
return lhs; }
189 inline friend vec3 operator/(
vec3 lhs,
float rhs) { lhs /= rhs;
return lhs; }
191 inline vec3& operator+=(
const vec3& rhs) {
192 value[0] += rhs.value[0];
193 value[1] += rhs.value[1];
194 value[2] += rhs.value[2];
198 inline vec3& operator-=(
const vec3& rhs) {
199 value[0] -= rhs.value[0];
200 value[1] -= rhs.value[1];
201 value[2] -= rhs.value[2];
205 inline vec3& operator*=(
const vec3& rhs) {
206 value[0] *= rhs.value[0];
207 value[1] *= rhs.value[1];
208 value[2] *= rhs.value[2];
212 inline vec3& operator/=(
const vec3& rhs) {
213 value[0] /= rhs.value[0];
214 value[1] /= rhs.value[1];
215 value[2] /= rhs.value[2];
219 inline vec3& operator+=(
float rhs) {
226 inline vec3& operator-=(
float rhs) {
233 inline vec3& operator*=(
float rhs) {
240 inline vec3& operator/=(
float rhs) {
247 inline friend bool operator==(
const vec3& a,
const vec3& b) {
248 return !memcmp(&a, &b,
sizeof(a));
251 inline friend bool operator!=(
const vec3& a,
const vec3& b) {
256 return{ (b.x < a.x) ? b.x : a.x,
257 (b.y < a.y) ? b.y : a.y,
258 (b.z < a.z) ? b.z : a.z };
262 return{ (a.x < b.x) ? b.x : a.x,
263 (a.y < b.y) ? b.y : a.y,
264 (a.z < b.z) ? b.z : a.z };
266 inline friend vec3 clamp(
const vec3& x,
const vec3& minVal,
const vec3& maxVal) {
267 return min_(max_(x, minVal), maxVal);
270 inline friend vec3 mix(
const vec3& a,
const vec3& b,
float t) {
271 return a * (1.0f - t) + b * t;
274 inline friend float length(
const vec3& a) {
275 return sqrtf(a.x * a.x + a.y * a.y + a.z * a.z);
278 inline friend float distance(
const vec3& a,
const vec3& b) {
279 return length(a - b);
282 inline friend vec3 normalize(
const vec3& a) {
283 return a / length(a);
286 inline friend float dot(
const vec3& a,
const vec3& b) {
287 return a.x * b.x + a.y * b.y + a.z * b.z;
297 struct {
float x, y, z, w; };
302 inline vec4(
float x,
float y,
float z,
float w) : x(x), y(y), z(z), w(w) {}
304 inline vec4(
float x) : x(x), y(x), z(x), w(x) {}
306 inline vec4(
const float x[4]) { memcpy(value, x,
sizeof(value)); }
308 inline friend bool operator==(
const vec4& a,
const vec4& b) {
309 return !memcmp(&a, &b,
sizeof(a));
312 inline friend bool operator!=(
const vec4& a,
const vec4& b) {
316 inline friend vec4 operator+(
vec4 lhs,
const vec4& rhs) { lhs += rhs;
return lhs; }
318 inline friend vec4 operator-(
vec4 lhs,
const vec4& rhs) { lhs -= rhs;
return lhs; }
320 inline friend vec4 operator*(
vec4 lhs,
const vec4& rhs) { lhs *= rhs;
return lhs; }
322 inline friend vec4 operator/(
vec4 lhs,
const vec4& rhs) { lhs /= rhs;
return lhs; }
324 inline friend vec4 operator+(
vec4 lhs,
float rhs) { lhs += rhs;
return lhs; }
326 inline friend vec4 operator-(
vec4 lhs,
float rhs) { lhs -= rhs;
return lhs; }
328 inline friend vec4 operator*(
vec4 lhs,
float rhs) { lhs *= rhs;
return lhs; }
330 inline friend vec4 operator/(
vec4 lhs,
float rhs) { lhs /= rhs;
return lhs; }
332 inline vec4& operator+=(
const vec4& rhs) {
333 value[0] += rhs.value[0];
334 value[1] += rhs.value[1];
335 value[2] += rhs.value[2];
336 value[3] += rhs.value[3];
340 inline vec4& operator-=(
const vec4& rhs) {
341 value[0] -= rhs.value[0];
342 value[1] -= rhs.value[1];
343 value[2] -= rhs.value[2];
344 value[3] -= rhs.value[3];
348 inline vec4& operator*=(
const vec4& rhs) {
349 value[0] *= rhs.value[0];
350 value[1] *= rhs.value[1];
351 value[2] *= rhs.value[2];
352 value[3] *= rhs.value[3];
356 inline vec4& operator/=(
const vec4& rhs) {
357 value[0] /= rhs.value[0];
358 value[1] /= rhs.value[1];
359 value[2] /= rhs.value[2];
360 value[3] /= rhs.value[3];
364 inline vec4& operator+=(
float rhs) {
372 inline vec4& operator-=(
float rhs) {
380 inline vec4& operator*=(
float rhs) {
388 inline vec4& operator/=(
float rhs) {
397 return{ (b.x < a.x) ? b.x : a.x,
398 (b.y < a.y) ? b.y : a.y,
399 (b.z < a.z) ? b.z : a.z,
400 (b.w < a.w) ? b.w : a.w };
404 return{ (a.x < b.x) ? b.x : a.x,
405 (a.y < b.y) ? b.y : a.y,
406 (a.z < b.z) ? b.z : a.z,
407 (a.w < b.w) ? b.w : a.w };
422 struct {
float left, top, right, bottom; };
425 static inline Rect MakeEmpty() {
431 inline float width()
const {
return right - left; }
432 inline float height()
const {
return bottom - top; }
433 inline float x()
const {
return left; }
434 inline float y()
const {
return top; }
435 inline float center_x()
const {
return (left + right) * 0.5f; }
436 inline float center_y()
const {
return (top + bottom) * 0.5f; }
438 inline Point origin()
const {
return { left, top }; }
440 inline void SetEmpty() {
441 memset(
this, 0,
sizeof(*
this));
444 inline bool IsEmpty()
const {
445 return *
this == MakeEmpty();
448 inline bool IsValid()
const {
449 return width() > 0 && height() > 0;
452 inline void Inset(
float dx,
float dy) {
459 inline void Outset(
float dx,
float dy) {
463 inline void Move(
float dx,
float dy) {
470 inline float area()
const {
471 return width() * height();
474 inline void Join(
const Rect& rhs) {
480 if (rhs.value[0] < value[0]) value[0] = rhs.value[0];
481 if (rhs.value[1] < value[1]) value[1] = rhs.value[1];
482 if (rhs.value[2] > value[2]) value[2] = rhs.value[2];
483 if (rhs.value[3] > value[3]) value[3] = rhs.value[3];
487 inline void Join(
const Point& p) {
490 *
this = { p.x, p.y, p.x, p.y };
493 if (p.x < value[0]) value[0] = p.x;
494 if (p.y < value[1]) value[1] = p.y;
495 if (p.x > value[2]) value[2] = p.x;
496 if (p.y > value[3]) value[3] = p.y;
500 inline bool Contains(
const Point& p)
const {
501 return p.x >= left && p.x <= right &&
502 p.y >= top && p.y <= bottom;
505 inline bool Contains(
const Rect& r)
const {
506 return left <= r.left && top <= r.top &&
507 right >= r.right && bottom >= r.bottom;
510 inline bool Intersects(
const Rect& rhs)
const {
511 return !(rhs.left > right ||
517 inline Rect Intersect(
const Rect& other)
const {
518 return{ (left < other.left) ? other.left : left,
519 (top < other.top) ? other.top : top,
520 (other.right < right) ? other.right : right,
521 (other.bottom < bottom) ? other.bottom : bottom };
524 friend inline bool operator==(
const Rect& a,
const Rect& b) {
525 return !memcmp(&a, &b,
sizeof(a));
528 friend inline bool operator!=(
const Rect& a,
const Rect& b) {
539 struct {
int left, top, right, bottom; };
542 static inline IntRect MakeEmpty() {
548 inline int width()
const {
return right - left; }
549 inline int height()
const {
return bottom - top; }
550 inline int x()
const {
return left; }
551 inline int y()
const {
return top; }
552 inline int center_x()
const {
return (
int)std::round((left + right) * 0.5f); }
553 inline int center_y()
const {
return (
int)std::round((top + bottom) * 0.5f); }
555 inline Point origin()
const {
return{ (float)left, (
float)top }; }
557 inline void SetEmpty() {
558 memset(
this, 0,
sizeof(*
this));
561 inline bool IsEmpty()
const {
562 return *
this == MakeEmpty();
565 inline bool IsValid()
const {
566 return width() > 0 && height() > 0;
569 inline void Inset(
int dx,
int dy) {
576 inline void Outset(
int dx,
int dy) {
580 inline void Move(
int dx,
int dy) {
587 inline int area()
const {
588 return width() * height();
591 inline void Join(
const IntRect& rhs) {
597 if (rhs.value[0] < value[0]) value[0] = rhs.value[0];
598 if (rhs.value[1] < value[1]) value[1] = rhs.value[1];
599 if (rhs.value[2] > value[2]) value[2] = rhs.value[2];
600 if (rhs.value[3] > value[3]) value[3] = rhs.value[3];
604 inline void Join(
const Point& p) {
607 *
this = { (int)std::floor(p.x), (int)std::floor(p.y), (int)std::ceil(p.x), (int)std::ceil(p.y) };
610 if ((
int)std::floor(p.x) < value[0]) value[0] = (
int)std::floor(p.x);
611 if ((
int)std::floor(p.y) < value[1]) value[1] = (int)std::floor(p.y);
612 if ((
int)std::ceil(p.x) > value[2]) value[2] = (
int)std::ceil(p.x);
613 if ((
int)std::ceil(p.y) > value[3]) value[3] = (int)std::ceil(p.y);
617 inline bool Contains(
const Point& p)
const {
618 return p.x >= left && p.x <= right &&
619 p.y >= top && p.y <= bottom;
622 inline bool Contains(
const IntRect& r)
const {
623 return left <= r.left && top <= r.top &&
624 right >= r.right && bottom >= r.bottom;
627 inline bool Intersects(
const IntRect& rhs)
const {
630 return !(rhs.left > right - 1 ||
632 rhs.top > bottom - 1 ||
637 return{ (left < other.left) ? other.left : left,
638 (top < other.top) ? other.top : top,
639 (other.right < right) ? other.right : right,
640 (other.bottom < bottom) ? other.bottom : bottom };
643 friend inline bool operator==(
const IntRect& a,
const IntRect& b) {
644 return !memcmp(&a, &b,
sizeof(a));
647 friend inline bool operator!=(
const IntRect& a,
const IntRect& b) {
662 bool IsRounded()
const;
665 float GetSignedDistance(
const Point& p)
const;
673 Rect CalculateInterior()
const;
Rounded Rectangle Helper.
This is a set of common JavaScriptCore Helper functions to simplify sample code.
vec2 Point
Point is typedef'd to a 2D vector.
Integer Rectangle Helper.