c# - Rotating a circle with mouse with direction -
i rotating circle mouse want there limit on how far circle can rotated. (lets 3 full times). when reaches it's limit can no longer turned in same direction, opposite direction can be. got stopping after max turns i'm trying find direction , every time mouse passes x-axis of circle direction changes because atan2 gives me angle relative x-axis. new mouse position in 1 quadrant , last position in quadrant subtracting these angles doesn't give me want. did explain well? suggestions?
private void helmpb_mousemove(object sender, mouseeventargs e) { if ((e.button != mousebuttons.left) || _dontturn) return; double angle = offsetangle(); point temp = mouseposition; float degrees = convert.tosingle(angle - _offsetangle); float diff = _lasthelmangle - degrees; float absdiff = math.abs(diff) % 360; if (absdiff > 180) absdiff = 360 - absdiff; double angle1 = math.atan2(_lasthelmpoint.y, _lasthelmpoint.x); if (angle1 < 0) angle1 += 2*math.pi; double angle2 = math.atan2(temp.y, temp.x); if (angle2 < 0) angle2 += 2*math.pi; double direction = angle1 - angle2; direction = direction*(180/math.pi); _deltahelmturn += convert.tosingle(absdiff); if (_deltahelmturn >= (_maxhelmturn*360.0)) { if (direction > 0 && _lasthelmdirection > 0) { _deltahelmturn = convert.tosingle(_maxhelmturn*360.0); degrees = convert.tosingle(_maxhelmturn*360.0)%360; } } _lasthelmdirection = direction; _lasthelmpoint = mouseposition; _lasthelmangle = convert.tosingle(degrees); _samehelmrotation = convert.tosingle(degrees); helmpb.image.dispose(); waterdepthplot.update(); helmpb.image = rotateimage(_originalhelmimage, -degrees); helmpb.update(); } private double offsetangle() { int helmxmid = helmpb.pointtoscreen(point.empty).x + (helmpb.width / 2); int helmymid = helmpb.pointtoscreen(point.empty).y + (helmpb.height / 2); double angle = anglefrompoints(mouseposition, new point(helmxmid, helmymid)); return angle; } private double anglefrompoints(point pt1, point pt2) { point p = new point(pt1.x - pt2.x, pt1.y - pt2.y); double alpha; if (p.y == 0) alpha = p.x > 0 ? 0d : 180d; else { double f = 1d * p.x / (math.sqrt(p.x * p.x + p.y * p.y)); alpha = math.acos(f) * 180d / math.pi; if (p.y > 0) alpha = 360d - alpha; } return alpha; }
the direction of rotation between 2 vectors equivalent sign of cross product.
//returns 1 ccw, -1 cw, 0 no change private int direction(point from, point to) { double cross = (from.x * to.y) - (from.y * to.x); return math.sign(cross); }
Comments
Post a Comment