# Fast approximate square root

**URL:** <https://discourse.processing.org/t/fast-approximate-square-root/12074>\
**Category:** Gallery\
**Created:** [June 15, 2019, 4:30am UTC](https://discourse.processing.org/t/fast-approximate-square-root/12074 "2019-06-15T04:30:12Z")\
**Posts on this page:** 1\
**Showing post:** 9

<div class="post-metadata">

**Author:** ![seanc4s](https://avatars.discourse-cdn.com/v4/letter/s/e19adc/32.png) [@seanc4s](https://discourse.processing.org/u/seanc4s)\
**Post date:** [June 16, 2019, 5:47am UTC](https://discourse.processing.org/t/fast-approximate-square-root/12074/9 "2019-06-16T05:47:53Z")

</div>

Very approximate atan2(…). I didn’t check edge cases like atan2(0,0) etc.

```auto
/**
 * Arctangent Bit Hack Version 
 * 
 * Move the mouse to change the direction of the eyes. 
 * The atan2() function computes the angle from each eye 
 * to the cursor. 
 */
 
Eye e1, e2, e3;

void setup() {
  size(640, 360);
  noStroke();
  e1 = new Eye( 250, 16, 120);
  e2 = new Eye( 164, 185, 80);  
  e3 = new Eye( 420, 230, 220);
}

void draw() {
  background(102);
  
  e1.update(mouseX, mouseY);
  e2.update(mouseX, mouseY);
  e3.update(mouseX, mouseY);

  e1.display();
  e2.display();
  e3.display();
}

class Eye {
  int x, y;
  int size;
  float angle = 0.0;
  
  Eye(int tx, int ty, int ts) {
    x = tx;
    y = ty;
    size = ts;
 }

  void update(int mx, int my) {
    angle = arctan2BH(my-y, mx-x); // Using hack
  }
  
  void display() {
    pushMatrix();
    translate(x, y);
    fill(255);
    ellipse(0, 0, size, size);
    rotate(angle);
    fill(153, 204, 0);
    ellipse(size/4, 0, size/2, size/2);
    popMatrix();
  }
}

float arctan2BH(float y, float x){
   float absY=Float.intBitsToFloat(Float.floatToRawIntBits(y) & 0x7fffffff);
   float absX=Float.intBitsToFloat(Float.floatToRawIntBits(x) & 0x7fffffff);
   float angle=min(absX,absY)*Float.intBitsToFloat(0x7EEEEEEE-Float.floatToRawIntBits(max(absX,absY)));
   if(absY>absX) angle=HALF_PI-angle;
   if(x<0) angle=PI-angle;
   if(y<0) angle=-angle;
   return angle;
}

```

---

_[View the full topic](https://discourse.processing.org/t/fast-approximate-square-root/12074)._
