Segment Intersection
Posted by ~Ray @ 2008-11-13 12:26:43
#include <iostream>#include <vector> #include "ofMain h"#include "ofVec2f h"#include "ofPoint2f h" //ambient light sensor reading#include <mach/mach h>#include <IOKit/IOKitLib h>#include <CoreFoundation/CoreFoundation h> enum { kGetSensorReadingID = 0. // getSensorReading(int * int *) kGetLEDBrightnessID = 1. // getLEDBrightness(int int *) kSetLEDBrightnessID = 2. // setLEDBrightness(int int int *) kSetLEDFadeID = 3. // setLEDFade(int int int int *) // other firmware-related functions // verifyFirmwareID = 4. // verifyFirmware(int *) // getFirmwareVersionID = 5. // getFirmwareVersion(int *) // other flashing-related functions // ...}; static double updateInterval = 0.1;static io_connect_t dataPort = 0; class world : public ofSimpleApp { public: void setup();void update();void draw(); void drawStroke(); void drawXPoints(); bool intersect(ofPoint2f P0a ofPoint2f P0b ofPoint2f P1a ofPoint2f P1b ofPoint2f &X); long getLight(); long light; vector<ofPoint2f> points; vector<float> thickVals; vector<ofPoint2f> xpoints; void keyPressed( int key );void mouseMoved( int x int y );void mouseDragged( int x int y int button );void mousePressed( int x int y int button );void mouseReleased(); }; void world::setup() { ofEnableAlphaBlending(); light = 100;} void world::update() {ofBackground( 100. 100. 100 ); light = getLight();} void world::draw() { ofSetColor( 0x222222 ); ofRect( 0. 550 ofGetWidth() ofGetHeight()-550 ); ofSetColor( 0xee0000 ); ofRect( 0 ofRangemap(400. 1400 ofGetHeight(). 0 light). 10. 10); drawStroke(); drawXPoints(); ofSetColor( 0xffffff );ofDrawBitmapString( "inkBrush.",20. 748 );} void world::drawStroke() { glBegin(GL_TRIANGLE_STRIP); for(int i=0; i<points size(); i++) { ofVec2f normal; if( i == 0) { normal set(0. 1); } else { normal = points[i] perpendicular(points[i-1]); } glColor4f(0.2. 0.2. 0.2. MAX(thickVals[i]. 0.1)); ofPoint2f normalPoint = points[i] + (normal*MAX(0.4. 10*(1-thickVals[i]))); ofPoint2f normalPointInv = points[i] + (-normal*MAX(0.4. 10*(1-thickVals[i]))); //glVertex2f(points[i] x points[i] y); glVertex2f(normalPoint x normalPoint y); glVertex2f(normalPointInv x normalPointInv y); //glVertex2f(points[i] x points[i] y); } glEnd();} void world::drawXPoints() { glColor3f(0.8. 0. 0); glPointSize(3.0); glBegin(GL_POINTS); for(int i=0; i<xpoints size(); i++) { glVertex2f(xpoints[i] x xpoints[i] y); } glEnd();} bool world::intersect(ofPoint2f P0a ofPoint2f P0b ofPoint2f P1a ofPoint2f P1b ofPoint2f &X) { float m0 = (P0b y - P0a y) / (P0b x - P0a x); float m1 = (P1b y - P1a y) / (P1b x - P1a x); float x = ( (P1a y - P0a y) + P0a x*m0 - P1a x*m1 ) / (m0 - m1); float y = P0a y + (x-P0a x)*m0; float left0 = MIN(P0a x. P0b x); float right0 = MAX(P0a x. P0b x); float left1 = MIN(P1a x. P1b x); float right1 = MAX(P1a x. P1b x); X x = x; X y = y; if ( x >= left0 && x <= right0 && x >= left1 && x <= right1) { return true; } else { return false; }} long world::getLight() { kern_return_t kr; IOItemCount scalarInputCount = 0; IOItemCount scalarOutputCount = 2; SInt32 left = 0 right = 0; kr = IOConnectMethodScalarIScalarO(dataPort kGetSensorReadingID scalarInputCount scalarOutputCount. &left. &right); if (kr == KERN_SUCCESS) { //cout << left << " " << right << endl; return (long)left; } //if (kr == kIOReturnBusy) // return; //mach_error("I/O Kit error:" kr); //exit(kr);} void world::keyPressed( int key ) { if( key == ' '){ ; }}void world::mouseMoved( int x int y ) { points push_back(ofPoint2f((float)x. (float)y)); thickVals push_back(ofRangemap(400. 1500. 1. 0 light)); //mark intersection if( points size() > 3 ) { for(int i=0; i<points size()-3; i++) { ofPoint2f X; bool doX = intersect(points[i] points[i+1] points[points size()-2] points[points size()-1]. X); if( doX ) { xpoints push_back(ofPoint2f(X)); } } }}void world::mouseDragged( int x int y int button ) {}void world::mousePressed( int x int y int button ) {}void world::mouseReleased() {} int main() { //set up ambient light sensor reading kern_return_t kr; io_service_t serviceObject; CFRunLoopTimerRef updateTimer; // Look up a registered IOService object whose class is AppleLMUController serviceObject = IOServiceGetMatchingService(kIOMasterPortDefault. IOServiceMatching("AppleLMUController")); if (!serviceObject) { fprintf(stderr. "failed to find ambient light sensor\n"); exit(1); } // Create a connection to the IOService object kr = IOServiceOpen(serviceObject mach_task_self(). 0. &dataPort); IOObjectRelease(serviceObject); if (kr != KERN_SUCCESS) { mach_error("IOServiceOpen:" kr); exit(kr); } ofSetupOpenGL( 1024. 768. OF_WINDOW );world APP;ofRunApp( &APP );}[ADVERTHERE]Related article:
http://blog.stefanix.net/segment-intersection
0 Comments:
No comments have been posted yet!
|