Help moving mouse over Bézier curve/spline

Discussion in 'Mac Programming' started by R0b0t, May 30, 2008.

  1. R0b0t macrumors newbie

    Joined:
    May 30, 2008
    #1
    I have this code which I'd imagine would work, so I don't really need help on that yet -- I need help getting the compiler to accept it.

    So, lets see, I'm using the newest Xcode, and am trying to compile this as a Console Tool. Most of the errors I'm getting when I try to build this have to do with the headers CGEvent and CGEventTypes not being found (but theres one about not being able to use a... function as a function :confused:), as you'll see if you try to build it. I'd also like a better alternative to my homebrew SleepMS function, if you could find one. (I saw some like nanosleep, but couldn't find out what a timespec struct looked like.)

    If someone could help me find out where those two header files are and maybe supply the information needed to use nanosleep or a similar function, and point out whatever else I've almost irreversibly screwed up, I will be a happy forum user :)

    Code:
    #include <iostream>
    #include <time.h>
    #include <math.h>
    #include "ApplicationServices/CoreGraphics/CGEvent.h"
    #include "ApplicationServices/CoreGraphics/CGEventTypes.h"
    
    int	    Distance(int x1, int y1, int x2, int y2);
    CGPoint PointOnCubicBezier(const CGPoint *cp, float t);
    CGPoint Spline(CGPoint* Result, int sx, int sy, int ex, int ey, CGPoint CtrlP1, CGPoint CtrlP2);
    void    BezierMouse(int x1, int y1, int x2, int y2, CGPoint p1, CGPoint p2);
    
    int main (int argc, char * const argv[]) {
    
    	CGPoint tmp1 = {500, 500}, tmp2 = {750, 150};
    	BezierMouse(100, 100, 1000, 1000, tmp1, tmp2);
        return 0;
    }
    
    int	Distance(int x1, int y1, int x2, int y2) {
    
    	return sqrt(pow((x2 - x1), 2)) + sqrt(pow((y2-y1), 2));
    }
    
    CGPoint PointOnCubicBezier(const CGPoint *cp, float t) {
    	
    	float zx, zy, bx, by, cx, cy, tSquared, tCubed, Curve;
    	int I;
    	CGPoint RBuffer;
    	
    	Curve = 3.0;
    	cx = Curve * (cp[1].x - cp[0].x);
    	bx = Curve * (cp[2].x - cp[1].x) - cx;
    	ax = cp[3].x - cp[0].x - cx - bx;
     
    	cy = Curve * (cp[1].y - cp[0].y);
    	by = Curve * (cp[2].y - cp[1].y) - cy;
    	ay = cp[3].y - cp[0].y - cy - by;
     
    	tSquared = t * t;
    	tCubed	 = t * t * t;
    	
    	RBuffer.x = round((ax * tCubed) + (bx * tSquared) + (cx * t) + cp[0].x);
    	RBuffer.y = round((ay * tCubed) + (by * tSquared) + (cy * t) + cp[0].y);
    	
    	return RBuffer;
    }
    
    int Spline(CGPoint* Result, int sx, int sy, int ex, int ey, CGPoint CtrlP1, CGPoint CtrlP2) {
    
    	float theta, theta_inc;
    	int Dist = 0, I = 0;
    	CGPoint CtrlPoints[] = {{sx, sy}, CtrlP1, CtrlP2, {ex, ey}};
    	
    	Dist = Distance(sx, sy, ex, ey);
    	theta_inc = 1.0 / Dist;
    	theta = 0.0;
    	
    	do {
    		theta_inc = min(1.0 - theta, theta_inc);
    		theta	  = theta + theta_inc;
    		Result[I] = PointOnCubicBezier(CtrlPoints, theta);
    		I++;
    	} while(theta >= 1.0);
    	
    	Result[I] = CtrlPoints[3];
    	return I;
    }
    
    void BezierMouse(int x1, int y1, int x2, int y2, CGPoint p1, CGPoint p2) {
    
    	CGPoint *Pt;
    	int I, J = 0;
    	timespec WaitT;
    	
    	WaitT.tv_sec  = 0;
    	WaitT.tv_nsec = 8000000;
    	
    	I = Spline(Pt, x1, y1, x2, y2, p1, p2);
    	
    	for(; J <= I; J++) {
    		CGEventCreateMouseEvent(NULL,
    		                        kCGEventMouseMoved,
    		                        Pt[J],
    								NULL);
    		nanosleep(&WaitT, NULL);
    	}
    }
     
  2. R0b0t thread starter macrumors newbie

    Joined:
    May 30, 2008
    #2
    I'm sure double posting is considered bad style here, but this is just to point out that I am not concerned with creating the spline, just finding the header files so I can include them. My problem might also be something else, and I'd need another person to diagnose that too.
     
  3. Muncher macrumors 65816

    Muncher

    Joined:
    Apr 19, 2007
    Location:
    California
    #3
    That looks like a headache :p. It appears that you're using one of the OpenGL libraries for your mouse input.

    Try adding the OpenGL framework to your project, see if that does it.
     
  4. R0b0t thread starter macrumors newbie

    Joined:
    May 30, 2008
    #4
    Maybe I should have mentioned that earlier... I got those functions from here.

    So its clearly in the Quart libraries somewhere :\ (pretty sure anyway)
     
  5. Columbo X macrumors member

    Joined:
    Jun 10, 2007
    Location:
    UK
    #5
    Quartz is just another name for CoreGraphics - check out the CoreGraphics framework.

    C
     
  6. Muncher macrumors 65816

    Muncher

    Joined:
    Apr 19, 2007
    Location:
    California
    #6
    That definitely sounds right. :D

    Don't listen to me. :p
     
  7. R0b0t thread starter macrumors newbie

    Joined:
    May 30, 2008
    #7
    How would I end up adding that to a command line tool? I might have forgot to mention I'm new to xcode.
     
  8. Columbo X macrumors member

    Joined:
    Jun 10, 2007
    Location:
    UK
    #8
    Hi R0b0t,

    To add the CoreGraphics framework right click on the Frameworks group folder (if there isn't one you can make your own if you want to keep the project organised by selecting New Group under the Project menu), select "Existing Frameworks" under the Add menu and navigate to /System/Library/Frameworks. Select ApplicationServices.framework to add it to your project (ApplicationServices is an umbrella framework and CoreGraphics is a framework within this).

    When done, simply add #include <CoreGraphics/CGEvent.h> or whatever to your source files.
     
  9. R0b0t thread starter macrumors newbie

    Joined:
    May 30, 2008
    #9
    Eh.... I tried that, but its still saying it couldn't find it. "No such file or directory"

    Appreciate your 'help', though. I bet it would usually work, I've looked into the .framework files, and those files are there, its just... Not working.



    Actually, it seems I might have fixed it. dragged the CoreGraphics folder into my project
     

Share This Page