xref: /haiku/src/apps/icon-o-matic/generic/support/support.h (revision 1deede7388b04dbeec5af85cae7164735ea9e70d)
1 /*
2  * Copyright 2006, Haiku.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Stephan Aßmus <superstippi@gmx.de>
7  */
8 
9 #ifndef SUPPORT_H
10 #define SUPPORT_H
11 
12 #include <Rect.h>
13 
14 class BPositionIO;
15 class BString;
16 
17 // constrain
18 inline void
19 constrain(float& value, float min, float max)
20 {
21 	if (value < min)
22 		value = min;
23 	if (value > max)
24 		value = max;
25 }
26 
27 // constrain_int32_0_255_asm
28 inline int32
29 constrain_int32_0_255_asm(int32 value) {
30     asm("movl  $0,    %%ecx;\n"
31         "movl  $255,  %%edx;\n"
32         "cmpl  %%ecx, %%eax;\n"
33         "cmovl %%ecx, %%eax;\n"
34         "cmpl  %%edx, %%eax;\n"
35         "cmovg %%edx, %%eax"
36        : "=a" (value)
37        : "a" (value)
38        : "%ecx", "%edx" );
39     return value;
40 }
41 
42 inline int32
43 constrain_int32_0_255_c(int32 value) {
44     return max_c(0, min_c(255, value));
45 }
46 
47 #define constrain_int32_0_255 constrain_int32_0_255_asm
48 
49 // rect_to_int
50 inline void
51 rect_to_int(BRect r,
52 			int32& left, int32& top, int32& right, int32& bottom)
53 {
54 	left = (int32)floorf(r.left);
55 	top = (int32)floorf(r.top);
56 	right = (int32)ceilf(r.right);
57 	bottom = (int32)ceilf(r.bottom);
58 }
59 
60 // point_point_distance
61 inline float
62 point_point_distance(BPoint a, BPoint b)
63 {
64 	float xDiff = b.x - a.x;
65 	float yDiff = b.y - a.y;
66 	return sqrtf(xDiff * xDiff + yDiff * yDiff);
67 }
68 
69 // point_line_distance
70 double
71 point_line_distance(double x1, double y1,
72 					double x2, double y2,
73 					double x,  double y);
74 
75 // point_line_distance
76 double
77 point_line_distance(BPoint point, BPoint a, BPoint b);
78 
79 // calc_angle
80 double
81 calc_angle(BPoint origin, BPoint from, BPoint to, bool degree = true);
82 
83 /*
84 template <class T>
85 T min4(const T a, const T b, const T c, const T d)
86 {
87 	T e = a < b ? a : b;
88 	T f = c < d ? c : d;
89 	return e < f ? e : f;
90 }
91 template <class T>
92 T max4(const T a, const T b, const T c, const T d)
93 {
94 	T e = a > b ? a : b;
95 	T f = c > d ? c : d;
96 	return e > f ? e : f;
97 }
98 */
99 inline float
100 min4(float a, float b, float c, float d)
101 {
102 	return min_c(a, min_c(b, min_c(c, d)));
103 }
104 
105 inline float
106 max4(float a, float b, float c, float d)
107 {
108 	return max_c(a, max_c(b, max_c(c, d)));
109 }
110 
111 inline float
112 min5(float v1, float v2, float v3, float v4, float v5)
113 {
114 	return min_c(min4(v1, v2, v3, v4), v5);
115 }
116 
117 inline float
118 max5(float v1, float v2, float v3, float v4, float v5)
119 {
120 	return max_c(max4(v1, v2, v3, v4), v5);
121 }
122 
123 inline float
124 roundf(float v)
125 {
126 	if (v >= 0.0)
127 		return floorf(v + 0.5);
128 	return ceilf(v - 0.5);
129 }
130 
131 status_t write_string(BPositionIO* stream, BString& string);
132 void append_float(BString& string, float n, int32 maxDigits = 4);
133 
134 //double gauss(double f);
135 
136 
137 # endif // SUPPORT_H
138