TuioPoint.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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 TuioTime currentTime;
  33. protected TuioTime startTime;
  34. protected float xpos;
  35. /**
  36. * Y coordinate, representated as a floating point value in a range of 0..1
  37. */
  38. protected float ypos;
  39. /**
  40. * The time stamp of the last update represented as TuioTime (time since session start)
  41. */
  42. /**
  43. * The creation time of this TuioPoint represented as TuioTime (time since session start)
  44. */
  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 float getAngle(float xp, float yp)
  105. {
  106. float side = xp - xpos;
  107. float height = yp - ypos;
  108. float distance = getDistance(xp, yp);
  109. float angle = (float)(Math.Asin(side / distance) + Math.PI / 2);
  110. if (height < 0) angle = 2.0f * (float)Math.PI - angle;
  111. return angle;
  112. }
  113. public float getAngle(TuioPoint tpoint)
  114. {
  115. return getAngle(tpoint.getX(), tpoint.getY());
  116. }
  117. public float getAngleDegrees(float xp, float yp)
  118. {
  119. return (getAngle(xp, yp) / (float)Math.PI) * 180.0f;
  120. }
  121. public float getAngleDegrees(TuioPoint tpoint)
  122. {
  123. return (getAngle(tpoint) / (float)Math.PI) * 180.0f;
  124. }
  125. public float getDistance(float x, float y)
  126. {
  127. float dx = xpos - x;
  128. float dy = ypos - y;
  129. return (float)Math.Sqrt(dx * dx + dy * dy);
  130. }
  131. public float getDistance(TuioPoint tpoint)
  132. {
  133. return getDistance(tpoint.getX(), tpoint.getY());
  134. }
  135. public int getScreenX(int width)
  136. {
  137. return (int)Math.Round(xpos * width);
  138. }
  139. public int getScreenY(int height)
  140. {
  141. return (int)Math.Round(ypos * height);
  142. }
  143. public TuioTime getStartTime()
  144. {
  145. return new TuioTime(startTime);
  146. }
  147. public TuioTime getTuioTime()
  148. {
  149. return new TuioTime(currentTime);
  150. }
  151. public float getX()
  152. {
  153. return xpos;
  154. }
  155. public float getY()
  156. {
  157. return ypos;
  158. }
  159. public void update(TuioPoint tpoint)
  160. {
  161. xpos = tpoint.getX();
  162. ypos = tpoint.getY();
  163. }
  164. /**
  165. * Takes two floating point coordinate arguments and updates its coordinate attributes
  166. * to the coordinates of the provided TuioPoint and leaves its time stamp unchanged.
  167. *
  168. * @param xp the X coordinate to assign
  169. * @param yp the Y coordinate to assign
  170. */
  171. public void update(float xp, float yp)
  172. {
  173. xpos = xp;
  174. ypos = yp;
  175. }
  176. /**
  177. * Takes a TuioTime object and two floating point coordinate arguments and updates its coordinate attributes
  178. * to the coordinates of the provided TuioPoint and its time stamp to the provided TUIO time object.
  179. *
  180. * @param ttime the TuioTime to assign
  181. * @param xp the X coordinate to assign
  182. * @param yp the Y coordinate to assign
  183. */
  184. public void update(TuioTime ttime, float xp, float yp)
  185. {
  186. xpos = xp;
  187. ypos = yp;
  188. currentTime = new TuioTime(ttime);
  189. }
  190. /**
  191. * Returns the X coordinate of this TuioPoint.
  192. * @return the X coordinate of this TuioPoint
  193. */
  194. /**
  195. * Returns the Y coordinate of this TuioPoint.
  196. * @return the Y coordinate of this TuioPoint
  197. */
  198. /**
  199. * Returns the distance to the provided coordinates
  200. *
  201. * @param xp the X coordinate of the distant point
  202. * @param yp the Y coordinate of the distant point
  203. * @return the distance to the provided coordinates
  204. */
  205. /**
  206. * Returns the distance to the provided TuioPoint
  207. *
  208. * @param tpoint the distant TuioPoint
  209. * @return the distance to the provided TuioPoint
  210. */
  211. /**
  212. * Returns the angle to the provided coordinates
  213. *
  214. * @param xp the X coordinate of the distant point
  215. * @param yp the Y coordinate of the distant point
  216. * @return the angle to the provided coordinates
  217. */
  218. /**
  219. * Returns the angle to the provided TuioPoint
  220. *
  221. * @param tpoint the distant TuioPoint
  222. * @return the angle to the provided TuioPoint
  223. */
  224. /**
  225. * Returns the angle in degrees to the provided coordinates
  226. *
  227. * @param xp the X coordinate of the distant point
  228. * @param yp the Y coordinate of the distant point
  229. * @return the angle in degrees to the provided TuioPoint
  230. */
  231. /**
  232. * Returns the angle in degrees to the provided TuioPoint
  233. *
  234. * @param tpoint the distant TuioPoint
  235. * @return the angle in degrees to the provided TuioPoint
  236. */
  237. /**
  238. * Returns the X coordinate in pixels relative to the provided screen width.
  239. *
  240. * @param width the screen width
  241. * @return the X coordinate of this TuioPoint in pixels relative to the provided screen width
  242. */
  243. /**
  244. * Returns the Y coordinate in pixels relative to the provided screen height.
  245. *
  246. * @param height the screen height
  247. * @return the Y coordinate of this TuioPoint in pixels relative to the provided screen height
  248. */
  249. /**
  250. * Returns the time stamp of this TuioPoint as TuioTime.
  251. *
  252. * @return the time stamp of this TuioPoint as TuioTime
  253. */
  254. /**
  255. * Returns the start time of this TuioPoint as TuioTime.
  256. *
  257. * @return the start time of this TuioPoint as TuioTime
  258. */
  259. }
  260. }