TuioPoint.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. * X coordinate, representated as a floating point value in a range of 0..1
  30. */
  31. protected float xpos;
  32. /**
  33. * Y coordinate, representated as a floating point value in a range of 0..1
  34. */
  35. protected float ypos;
  36. /**
  37. * The time stamp of the last update represented as TuioTime (time since session start)
  38. */
  39. protected TuioTime currentTime;
  40. /**
  41. * The creation time of this TuioPoint represented as TuioTime (time since session start)
  42. */
  43. protected TuioTime startTime;
  44. /**
  45. * The default constructor takes no arguments and sets
  46. * its coordinate attributes to zero and its time stamp to the current session time.
  47. */
  48. public TuioPoint () {
  49. xpos = 0.0f;
  50. ypos = 0.0f;
  51. currentTime = TuioTime.getSessionTime();
  52. startTime = new TuioTime(currentTime);
  53. }
  54. /**
  55. * This constructor takes two floating point coordinate arguments and sets
  56. * its coordinate attributes to these values and its time stamp to the current session time.
  57. *
  58. * @param xp the X coordinate to assign
  59. * @param yp the Y coordinate to assign
  60. */
  61. public TuioPoint (float xp, float yp) {
  62. xpos = xp;
  63. ypos = yp;
  64. currentTime = TuioTime.getSessionTime();
  65. startTime = new TuioTime(currentTime);
  66. }
  67. /**
  68. * This constructor takes a TuioPoint argument and sets its coordinate attributes
  69. * to the coordinates of the provided TuioPoint and its time stamp to the current session time.
  70. *
  71. * @param tpoint the TuioPoint to assign
  72. */
  73. public TuioPoint(TuioPoint tpoint) {
  74. xpos = tpoint.getX();
  75. ypos = tpoint.getY();
  76. currentTime = TuioTime.getSessionTime();
  77. startTime = new TuioTime(currentTime);
  78. }
  79. /**
  80. * This constructor takes a TuioTime object and two floating point coordinate arguments and sets
  81. * its coordinate attributes to these values and its time stamp to the provided TUIO time object.
  82. *
  83. * @param ttime the TuioTime to assign
  84. * @param xp the X coordinate to assign
  85. * @param yp the Y coordinate to assign
  86. */
  87. public TuioPoint(TuioTime ttime, float xp, float yp) {
  88. xpos = xp;
  89. ypos = yp;
  90. currentTime = new TuioTime(ttime);
  91. startTime = new TuioTime(currentTime);
  92. }
  93. /**
  94. * Takes a TuioPoint argument and updates its coordinate attributes
  95. * to the coordinates of the provided TuioPoint and leaves its time stamp unchanged.
  96. *
  97. * @param tpoint the TuioPoint to assign
  98. */
  99. public void update(TuioPoint tpoint) {
  100. xpos = tpoint.getX();
  101. ypos = tpoint.getY();
  102. }
  103. /**
  104. * Takes two floating point coordinate arguments and updates its coordinate attributes
  105. * to the coordinates of the provided TuioPoint and leaves its time stamp unchanged.
  106. *
  107. * @param xp the X coordinate to assign
  108. * @param yp the Y coordinate to assign
  109. */
  110. public void update(float xp, float yp) {
  111. xpos = xp;
  112. ypos = yp;
  113. }
  114. /**
  115. * Takes a TuioTime object and two floating point coordinate arguments and updates its coordinate attributes
  116. * to the coordinates of the provided TuioPoint and its time stamp to the provided TUIO time object.
  117. *
  118. * @param ttime the TuioTime to assign
  119. * @param xp the X coordinate to assign
  120. * @param yp the Y coordinate to assign
  121. */
  122. public void update(TuioTime ttime, float xp, float yp) {
  123. xpos = xp;
  124. ypos = yp;
  125. currentTime = new TuioTime(ttime);
  126. }
  127. /**
  128. * Returns the X coordinate of this TuioPoint.
  129. * @return the X coordinate of this TuioPoint
  130. */
  131. public float getX() {
  132. return xpos;
  133. }
  134. /**
  135. * Returns the Y coordinate of this TuioPoint.
  136. * @return the Y coordinate of this TuioPoint
  137. */
  138. public float getY() {
  139. return ypos;
  140. }
  141. /**
  142. * Returns the distance to the provided coordinates
  143. *
  144. * @param xp the X coordinate of the distant point
  145. * @param yp the Y coordinate of the distant point
  146. * @return the distance to the provided coordinates
  147. */
  148. public float getDistance(float x, float y) {
  149. float dx = xpos-x;
  150. float dy = ypos-y;
  151. return (float)Math.Sqrt(dx*dx+dy*dy);
  152. }
  153. /**
  154. * Returns the distance to the provided TuioPoint
  155. *
  156. * @param tpoint the distant TuioPoint
  157. * @return the distance to the provided TuioPoint
  158. */
  159. public float getDistance(TuioPoint tpoint) {
  160. return getDistance(tpoint.getX(),tpoint.getY());
  161. }
  162. /**
  163. * Returns the angle to the provided coordinates
  164. *
  165. * @param xp the X coordinate of the distant point
  166. * @param yp the Y coordinate of the distant point
  167. * @return the angle to the provided coordinates
  168. */
  169. public float getAngle(float xp, float yp) {
  170. float side = xp-xpos;
  171. float height = yp- ypos;
  172. float distance = getDistance(xp,yp);
  173. float angle = (float)(Math.Asin(side/distance)+Math.PI/2);
  174. if (height<0) angle = 2.0f*(float)Math.PI-angle;
  175. return angle;
  176. }
  177. /**
  178. * Returns the angle to the provided TuioPoint
  179. *
  180. * @param tpoint the distant TuioPoint
  181. * @return the angle to the provided TuioPoint
  182. */
  183. public float getAngle(TuioPoint tpoint) {
  184. return getAngle(tpoint.getX(),tpoint.getY());
  185. }
  186. /**
  187. * Returns the angle in degrees to the provided coordinates
  188. *
  189. * @param xp the X coordinate of the distant point
  190. * @param yp the Y coordinate of the distant point
  191. * @return the angle in degrees to the provided TuioPoint
  192. */
  193. public float getAngleDegrees(float xp, float yp) {
  194. return (getAngle(xp,yp)/(float)Math.PI)*180.0f;
  195. }
  196. /**
  197. * Returns the angle in degrees to the provided TuioPoint
  198. *
  199. * @param tpoint the distant TuioPoint
  200. * @return the angle in degrees to the provided TuioPoint
  201. */
  202. public float getAngleDegrees(TuioPoint tpoint) {
  203. return (getAngle(tpoint)/(float)Math.PI)*180.0f;
  204. }
  205. /**
  206. * Returns the X coordinate in pixels relative to the provided screen width.
  207. *
  208. * @param width the screen width
  209. * @return the X coordinate of this TuioPoint in pixels relative to the provided screen width
  210. */
  211. public int getScreenX(int width) {
  212. return (int)Math.Round(xpos*width);
  213. }
  214. /**
  215. * Returns the Y coordinate in pixels relative to the provided screen height.
  216. *
  217. * @param height the screen height
  218. * @return the Y coordinate of this TuioPoint in pixels relative to the provided screen height
  219. */
  220. public int getScreenY(int height) {
  221. return (int)Math.Round(ypos*height);
  222. }
  223. /**
  224. * Returns the time stamp of this TuioPoint as TuioTime.
  225. *
  226. * @return the time stamp of this TuioPoint as TuioTime
  227. */
  228. public TuioTime getTuioTime() {
  229. return new TuioTime(currentTime);
  230. }
  231. /**
  232. * Returns the start time of this TuioPoint as TuioTime.
  233. *
  234. * @return the start time of this TuioPoint as TuioTime
  235. */
  236. public TuioTime getStartTime() {
  237. return new TuioTime(startTime);
  238. }
  239. }
  240. }