mouseevent - Making eyeball look at/follow cursor movement (C#) -


i'm stuck here. want have eyes drawn while 'looking' @ (the angle of) cursor. also, should contained within bigger circle/quadrant (just eyeball). sadly, won't draw eye me @ right position/angle , @ every mouse movement. thing draw ellipse @ (0,0), that's not want.

my idea calculate ratio of triangles pythagorean theorem. apply right coordinates (with correct ratio) in drawellipse(); method. should repeated everytime move cursor.

you can check image mathematical reasoning. enter image description here

here code, note panel made in designer mode isn't included in code, shouldn't big deal:

using system; using system.collections.generic; using system.componentmodel; using system.data; using system.drawing; using system.linq; using system.text; using system.threading.tasks; using system.windows.forms;  namespace oogjes2 {     public partial class form1 : form     {         public int mousex;         public int mousey;          public form1()         {             initializecomponent();             panel1.paint += paintpanel;             panel1.mousemove += panel1_mousemove;              }          //panel1 cover screen (0.0) , onwards,         void panel1_mousemove(object sender, mouseeventargs mea)         {             int mousex = mea.x;             int mousey = mea.y;         }           void paintpanel(object obj, painteventargs pea)         {             pen blackpen = new pen(color.black, 3);              // black outer circle doesnt move             pea.graphics.drawellipse(blackpen, -125, -125, 250, 250);              // e = 63. diagonal distance (0,0) starting point drawellipse             // factor = multiplication mea.x , mea.y respective ex , ey of small circle.             // ey = factor * mousex (mea.x). same ex.              float e = (float)math.sqrt(45 * 45 + 45 * 45); //=63             float factor = (e / (float)math.sqrt(mousex * mousex + mousey * mousey));             int ex = mousex * (int)factor;             int ey = mousey * (int)factor;              //  eye should redrawn @ every mousemovement             pea.graphics.drawellipse(blackpen, ex, ey, 50, 50);             this.invalidate();         }     } } 

try out:

enter image description here

public partial class form1 : form {      private timer tmr;     private int pupilradius = 20;     private int eyeballradius = 50;     private int distancebetweeneyes = 20;      public form1()     {         initializecomponent();         this.panel1.paint += panel1_paint;          tmr = new timer();         tmr.interval = 100;         tmr.tick += tmr_tick;         tmr.start();     }      void tmr_tick(object sender, eventargs e)     {         panel1.invalidate();     }      void panel1_paint(object sender, painteventargs e)     {         point center = new point(panel1.clientsize.width / 2, panel1.clientsize.height / 2);         point lefteyecenter = new point(center.x - eyeballradius - (distancebetweeneyes / 2), center.y);         point righteyecenter = new point(center.x + eyeballradius + (distancebetweeneyes / 2), center.y);          rectangle rc = new rectangle(lefteyecenter, new size(1, 1));         rc.inflate(eyeballradius, eyeballradius);         e.graphics.drawellipse(pens.black, rc);          rc = new rectangle(righteyecenter, new size(1, 1));         rc.inflate(eyeballradius, eyeballradius);         e.graphics.drawellipse(pens.black, rc);          point curpos = panel1.pointtoclient(cursor.position);         double distancefromlefteyetocursor = getdistance(lefteyecenter.x, lefteyecenter.y, curpos.x, curpos.y);         double distancefromrighteyetocursor = getdistance(righteyecenter.x, righteyecenter.y, curpos.x, curpos.y);         double angleleft = getangleindegrees(lefteyecenter.x, lefteyecenter.y, curpos.x, curpos.y);         double angleright = getangleindegrees(righteyecenter.x, righteyecenter.y, curpos.x, curpos.y);          rc = new rectangle(new point(math.min((int)distancefromlefteyetocursor, eyeballradius - pupilradius), 0), new size(1, 1));         rc.inflate(pupilradius, pupilradius);         e.graphics.translatetransform(lefteyecenter.x, lefteyecenter.y);         e.graphics.rotatetransform((float)angleleft);         e.graphics.fillellipse(brushes.blue, rc);          rc = new rectangle(new point(math.min((int)distancefromrighteyetocursor, eyeballradius - pupilradius), 0), new size(1, 1));         rc.inflate(pupilradius, pupilradius);         e.graphics.resettransform();         e.graphics.translatetransform(righteyecenter.x, righteyecenter.y);         e.graphics.rotatetransform((float)angleright);         e.graphics.fillellipse(brushes.blue, rc);     }      private double getdistance(int ax, int ay, int bx, int by)     {         return math.sqrt(math.pow((double)ax - bx, 2) + math.pow((double)ay - by, 2));     }      private double getangleindegrees(int cx, int cy, int x, int y)     {         // draw line center of circle         // cursor is...         // if line points:         // = 0 degrees         // right = 90 degrees         // down = 180 degrees         // left = 270 degrees          double angle;         int dy = y - cy;         int dx = x - cx;         if (dx == 0) // straight , down | avoid divide 0 error!         {             if (dy <= 0)             {                 angle = 0;             }             else             {                 angle = 180;             }         }         else         {             angle = math.atan((double)dy / (double)dx);             angle = angle * ((double)180 / math.pi);              if (x <= cx)             {                 angle = 180 + angle;             }         }          return angle;     }  } 

Comments

Popular posts from this blog

qt - Using float or double for own QML classes -

Create Outlook appointment via C# .Net -

ios - Swift Array Resetting Itself -