/src/Line.cpp

https://github.com/jtnimoy/Bandwidth · C++ · 195 lines · 112 code · 56 blank · 27 comment · 13 complexity · 5c570a577acd4f5bcd00ddc3de80cfdc MD5 · raw file

  1. /*
  2. * JTLine.cpp
  3. * emptyExample
  4. *
  5. // This work is licensed under the Creative Commons
  6. // Attribution-NonCommercial-ShareAlike 3.0 Unported License. To view a
  7. // copy of this license, visit
  8. // http://creativecommons.org/licenses/by-nc-sa/3.0/ or send a letter to
  9. // Creative Commons, 444 Castro Street, Suite 900, Mountain View,
  10. // California, 94041, USA.
  11. //
  12. *
  13. */
  14. #include "Line.h"
  15. #include "testApp.h"
  16. JTLine::JTLine(){
  17. }
  18. //--------------------------------------------
  19. JTLine::JTLine(int xx,int yy){
  20. x=xx;
  21. y=yy;
  22. x2 = ofGetScreenWidth() * ofRandomuf();
  23. y2 = ofGetScreenHeight() * ofRandomuf();
  24. dest_x2 = ofGetScreenWidth() * ofRandomuf();
  25. dest_y2 = ofGetScreenHeight() * ofRandomuf();
  26. ticks=0;
  27. death = -1;
  28. color = MColor( ofRandomuf() * HALF_PI + PI, 0.5, 0.5, "HSB");
  29. ballColor = MColor( ofRandomuf() * PI, 1.0f, 1.0f, "HSB");
  30. lineWidth = ofRandomuf() * 10;
  31. ballSpeed = ofRandomuf() * 100.0f + 200;
  32. ballX = ballY = 0;
  33. x3 = y3 = x4 = y4 = 0;
  34. radius = lineWidth;
  35. pluckCounter = 0;
  36. }
  37. //--------------------------------------------
  38. void JTLine::step(){
  39. x2 += (dest_x2 - x2) * 0.1;
  40. y2 += (dest_y2 - y2) * 0.1;
  41. ballPosition = ((ticks % ballSpeed) / (float)ballSpeed);
  42. //find sufficient extender length
  43. float hyp = ofDist(ofGetScreenWidth(),ofGetScreenHeight(),0,0);
  44. //get angles in both directions
  45. float theta1 = atan2(y-y2,x-x2);
  46. float theta2 = atan2(y2-y,x2-x);
  47. //define rendering segment
  48. x3 = x + cos(theta2) * hyp;
  49. y3 = y + sin(theta2) * hyp;
  50. x4 = x2 + cos(theta1) * hyp;
  51. y4 = y2 + sin(theta1) * hyp;
  52. //find ball position based on time
  53. ballX = x3 + (x4-x3) * ballPosition;
  54. ballY = y3 + (y4-y3) * ballPosition;
  55. //update math calculations
  56. slope = (y4 - y3) / (x4 - x3);
  57. elevation = y4 - slope * x4;
  58. vector<JTLine*>::iterator it;
  59. float screenW = ofGetScreenWidth();
  60. float screenH = ofGetScreenHeight();
  61. for(it = SceneLines::instance->lines.begin(); it != SceneLines::instance->lines.end() ; it ++ ){
  62. if( (*it) != this){
  63. if(ballX > 0 && ballX < screenW && ballY > 0 && ballY < screenH ){
  64. bool side = (*it)->pointAbove(ballX,ballY);
  65. if(sides[*it] == 2 ){
  66. if(side){
  67. sides[*it] = 3;
  68. radius += 20;
  69. (*it)->crossed();
  70. sound();
  71. }
  72. }else{
  73. if(!side){
  74. sides[*it] = 2;
  75. radius += 20;
  76. (*it)->crossed();
  77. sound();
  78. }
  79. }
  80. }else{
  81. sides[*it] = 2;
  82. }
  83. }
  84. }
  85. pluckCounter = max(0,pluckCounter-1);
  86. radius = fmin(fmax(lineWidth, radius-5),50);
  87. ticks++;
  88. }
  89. //--------------------------------------------
  90. void JTLine::draw(float alpha){
  91. //draw line
  92. ofNoFill();
  93. glPushAttrib(GL_ENABLE_BIT);
  94. glEnable(GL_LINE_SMOOTH);
  95. glHint(GL_LINE_SMOOTH_HINT,GL_NICEST);
  96. ofSetLineWidth(this->lineWidth);
  97. glBegin(GL_LINES);
  98. if(pluckCounter==0){
  99. glColor4b(
  100. color.red() * 255.0,
  101. color.green() * 255.0,
  102. color.blue() * 255.0,
  103. 128.0 * alpha );
  104. }else{
  105. glColor4f(1,1,1,alpha);
  106. }
  107. glVertex2f(x3,y3);
  108. glColor4b(0,0,0,0);
  109. glVertex2f(x4,y4);
  110. glEnd();
  111. glPopAttrib();
  112. //draw ball
  113. ofSetColor(
  114. ballColor.red() * 255.0,
  115. ballColor.green() * 255.0,
  116. ballColor.blue() * 255.0,
  117. 128.0 * alpha );
  118. ofFill();
  119. ofSetLineWidth(1);
  120. ofCircle(ballX,ballY,radius);
  121. }
  122. //--------------------------------------------
  123. bool JTLine::pointAbove(float xx,float yy){
  124. return slope * xx + elevation > yy;
  125. }
  126. //--------------------------------------------
  127. void JTLine::sound(){
  128. SceneLines::instance->snd0.setSpeed(
  129. SceneLines::instance->welltempered[
  130. (int)((1.0 - (ballY / ofGetScreenHeight() ) ) * 12)
  131. ] * 0.5);
  132. SceneLines::instance->snd0.setVolume(0.1);
  133. SceneLines::instance->snd0.play();
  134. }
  135. //--------------------------------------------
  136. void JTLine::crossed(){
  137. pluckCounter+=2;
  138. }