TuioPoint.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /*
  2. TUIO C# Library - part of the reacTIVision project
  3. http://reactivision.sourceforge.net/
  4. Copyright (c) 2005-2009 Martin Kaltenbrunner <mkalten@iua.upf.edu>
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. */
  17. using System;
  18. namespace TUIO
  19. {
  20. /**
  21. * The TuioPoint class on the one hand is a simple container and utility class to handle TUIO positions in general,
  22. * on the other hand the TuioPoint is the base class for the TuioCursor and TuioObject classes.
  23. *
  24. * @author Martin Kaltenbrunner
  25. * @version 1.4
  26. */
  27. public class TuioPoint
  28. {
  29. /**
  30. * X coordinate, representated as a floating point value in a range of 0..1
  31. */
  32. protected float xpos;
  33. /**
  34. * Y coordinate, representated as a floating point value in a range of 0..1
  35. */
  36. protected float ypos;
  37. /**
  38. * The time stamp of the last update represented as TuioTime (time since session start)
  39. */
  40. protected TuioTime currentTime;
  41. /**
  42. * The creation time of this TuioPoint represented as TuioTime (time since session start)
  43. */
  44. protected TuioTime startTime;
  45. /**
  46. * The default constructor takes no arguments and sets
  47. * its coordinate attributes to zero and its time stamp to the current session time.
  48. */
  49. public TuioPoint()
  50. {
  51. xpos = 0.0f;
  52. ypos = 0.0f;
  53. currentTime = TuioTime.getSessionTime();
  54. startTime = new TuioTime(currentTime);
  55. }
  56. /**
  57. * This constructor takes two floating point coordinate arguments and sets
  58. * its coordinate attributes to these values and its time stamp to the current session time.
  59. *
  60. * @param xp the X coordinate to assign
  61. * @param yp the Y coordinate to assign
  62. */
  63. public TuioPoint(float xp, float yp)
  64. {
  65. xpos = xp;
  66. ypos = yp;
  67. currentTime = TuioTime.getSessionTime();
  68. startTime = new TuioTime(currentTime);
  69. }
  70. /**
  71. * This constructor takes a TuioPoint argument and sets its coordinate attributes
  72. * to the coordinates of the provided TuioPoint and its time stamp to the current session time.
  73. *
  74. * @param tpoint the TuioPoint to assign
  75. */
  76. public TuioPoint(TuioPoint tpoint)
  77. {
  78. xpos = tpoint.getX();
  79. ypos = tpoint.getY();
  80. currentTime = TuioTime.getSessionTime();
  81. startTime = new TuioTime(currentTime);
  82. }
  83. /**
  84. * This constructor takes a TuioTime object and two floating point coordinate arguments and sets
  85. * its coordinate attributes to these values and its time stamp to the provided TUIO time object.
  86. *
  87. * @param ttime the TuioTime to assign
  88. * @param xp the X coordinate to assign
  89. * @param yp the Y coordinate to assign
  90. */
  91. public TuioPoint(TuioTime ttime, float xp, float yp)
  92. {
  93. xpos = xp;
  94. ypos = yp;
  95. currentTime = new TuioTime(ttime);
  96. startTime = new TuioTime(currentTime);
  97. }
  98. /**
  99. * Takes a TuioPoint argument and updates its coordinate attributes
  100. * to the coordinates of the provided TuioPoint and leaves its time stamp unchanged.
  101. *
  102. * @param tpoint the TuioPoint to assign
  103. */
  104. public void update(TuioPoint tpoint)
  105. {
  106. xpos = tpoint.getX();
  107. ypos = tpoint.getY();
  108. }
  109. /**
  110. * Takes two floating point coordinate arguments and updates its coordinate attributes
  111. * to the coordinates of the provided TuioPoint and leaves its time stamp unchanged.
  112. *
  113. * @param xp the X coordinate to assign
  114. * @param yp the Y coordinate to assign
  115. */
  116. public void update(float xp, float yp)
  117. {
  118. xpos = xp;
  119. ypos = yp;
  120. }
  121. /**
  122. * Takes a TuioTime object and two floating point coordinate arguments and updates its coordinate attributes
  123. * to the coordinates of the provided TuioPoint and its time stamp to the provided TUIO time object.
  124. *
  125. * @param ttime the TuioTime to assign
  126. * @param xp the X coordinate to assign
  127. * @param yp the Y coordinate to assign
  128. */
  129. public void update(TuioTime ttime, float xp, float yp)
  130. {
  131. xpos = xp;
  132. ypos = yp;
  133. currentTime = new TuioTime(ttime);
  134. }
  135. /**
  136. * Returns the X coordinate of this TuioPoint.
  137. * @return the X coordinate of this TuioPoint
  138. */
  139. public float getX()
  140. {
  141. return xpos;
  142. }
  143. /**
  144. * Returns the Y coordinate of this TuioPoint.
  145. * @return the Y coordinate of this TuioPoint
  146. */
  147. public float getY()
  148. {
  149. return ypos;
  150. }
  151. /**
  152. * Returns the distance to the provided coordinates
  153. *
  154. * @param xp the X coordinate of the distant point
  155. * @param yp the Y coordinate of the distant point
  156. * @return the distance to the provided coordinates
  157. */
  158. public float getDistance(float x, float y)
  159. {
  160. float dx = xpos - x;
  161. float dy = ypos - y;
  162. return (float)Math.Sqrt(dx * dx + dy * dy);
  163. }
  164. /**
  165. * Returns the distance to the provided TuioPoint
  166. *
  167. * @param tpoint the distant TuioPoint
  168. * @return the distance to the provided TuioPoint
  169. */
  170. public float getDistance(TuioPoint tpoint)
  171. {
  172. return getDistance(tpoint.getX(), tpoint.getY());
  173. }
  174. /**
  175. * Returns the angle to the provided coordinates
  176. *
  177. * @param xp the X coordinate of the distant point
  178. * @param yp the Y coordinate of the distant point
  179. * @return the angle to the provided coordinates
  180. */
  181. public float getAngle(float xp, float yp)
  182. {
  183. float side = xp - xpos;
  184. float height = yp - ypos;
  185. float distance = getDistance(xp, yp);
  186. float angle = (float)(Math.Asin(side / distance) + Math.PI / 2);
  187. if (height < 0) angle = 2.0f * (float)Math.PI - angle;
  188. return angle;
  189. }
  190. /**
  191. * Returns the angle to the provided TuioPoint
  192. *
  193. * @param tpoint the distant TuioPoint
  194. * @return the angle to the provided TuioPoint
  195. */
  196. public float getAngle(TuioPoint tpoint)
  197. {
  198. return getAngle(tpoint.getX(), tpoint.getY());
  199. }
  200. /**
  201. * Returns the angle in degrees to the provided coordinates
  202. *
  203. * @param xp the X coordinate of the distant point
  204. * @param yp the Y coordinate of the distant point
  205. * @return the angle in degrees to the provided TuioPoint
  206. */
  207. public float getAngleDegrees(float xp, float yp)
  208. {
  209. return (getAngle(xp, yp) / (float)Math.PI) * 180.0f;
  210. }
  211. /**
  212. * Returns the angle in degrees to the provided TuioPoint
  213. *
  214. * @param tpoint the distant TuioPoint
  215. * @return the angle in degrees to the provided TuioPoint
  216. */
  217. public float getAngleDegrees(TuioPoint tpoint)
  218. {
  219. return (getAngle(tpoint) / (float)Math.PI) * 180.0f;
  220. }
  221. /**
  222. * Returns the X coordinate in pixels relative to the provided screen width.
  223. *
  224. * @param width the screen width
  225. * @return the X coordinate of this TuioPoint in pixels relative to the provided screen width
  226. */
  227. public int getScreenX(int width)
  228. {
  229. return (int)Math.Round(xpos * width);
  230. }
  231. /**
  232. * Returns the Y coordinate in pixels relative to the provided screen height.
  233. *
  234. * @param height the screen height
  235. * @return the Y coordinate of this TuioPoint in pixels relative to the provided screen height
  236. */
  237. public int getScreenY(int height)
  238. {
  239. return (int)Math.Round(ypos * height);
  240. }
  241. /**
  242. * Returns the time stamp of this TuioPoint as TuioTime.
  243. *
  244. * @return the time stamp of this TuioPoint as TuioTime
  245. */
  246. public TuioTime getTuioTime()
  247. {
  248. return new TuioTime(currentTime);
  249. }
  250. /**
  251. * Returns the start time of this TuioPoint as TuioTime.
  252. *
  253. * @return the start time of this TuioPoint as TuioTime
  254. */
  255. public TuioTime getStartTime()
  256. {
  257. return new TuioTime(startTime);
  258. }
  259. }
  260. }