Way to round edges of objects opensCAD - openscad

Is there an easy way/function to round edges for an openscad object?
e.g. round the edges of the cylinders

minkowski() is your friend for rounding over all edges of a geometry. minkowski() is also incredibly slow and should only be used for final rendering. You can also implement primitives which have rounded edges more efficiently with other constructs.
$fn=60;
module drawLedgeRing()
{
difference()
{
cylinder(4,10,10);
translate([0,0,-1])
cylinder(4,6,6);
translate([0,0,2])
cylinder(4,8,8);
}
}
minkowski()
{
drawLedgeRing();
sphere(.25);
}
//drawLedgeRing();

I was looking for a radiused block to 3d print an instrument case.
After reading the earlier answers I looked into hull (Not the town! :-)
making 8 identical spheres at the corners of a block and hulling them looks good.
module radiusedblock(xlen,ylen,zlen,radius){
hull(){
translate([radius,radius,radius]) sphere(r=radius);
translate([xlen + radius , radius , radius]) sphere(r=radius);
translate([radius , ylen + radius , radius]) sphere(r=radius);
translate([xlen + radius , ylen + radius , radius]) sphere(r=radius);
translate([radius , radius , zlen + radius]) sphere(r=radius);
translate([xlen + radius , radius , zlen + radius]) sphere(r=radius);
translate([radius,ylen + radius,zlen + radius]) sphere(r=radius);
translate([xlen + radius,ylen + radius,zlen + radius]) sphere(r=radius);
}
}
radiusedblock(30,40,50,5);

There are probably many ways to make a rounded cylinder. One way is to make 2 donut shaped objects and hull them
hull(){
rotate_extrude() translate([r1,0,0]) circle(r2);
rotate_extrude() translate([r1,0,h1]) circle(r2);
}

To round a cylinder, you should use something like HULL command for two spheres.
It will make a tube where each sphere is the cap of the tube, by wrapping them into a new object.
You can use that to round your cylinder with minkowski.
minkowski between the cylinder and the rounded tube. If you merge a sphere with a cube, it will also round the long tube zone and make it pregnant. Hull is very useful, you can do 100ds of hull commands instead of an extrusion for complex stuff for example.
Also check Fibonaci sphere from thingiverse for an interesting sphere, although it isn't symmetrical as is best on a tube.

Try this cube with all sides rounded:
$fn=32;
border=5;
minkowski(){
cube([10,20,5],center=false);
rotate([90,0,90]) cylinder(h=10,r=border);
cylinder(h=0.1,r=border);
}

I needed the same thing today, and the answers here were only semi-useful, so I implemented my own module. Feel free to use/share :)
module roundedcube(xx, yy, height, radius) {
difference(){
cube([xx,yy,height]);
difference(){
translate([-.5,-.5,-.2])
cube([radius+.5,radius+.5,height+.5]);
translate([radius,radius,height/2])
cylinder(height,radius,radius,true);
}
translate([xx,0,0])
rotate(90)
difference(){
translate([-.5,-.5,-.2])
cube([radius+.5,radius+.5,height+.5]);
translate([radius,radius,height/2])
cylinder(height,radius,radius,true);
}
translate([xx,yy,0])
rotate(180)
difference(){
translate([-.5,-.5,-.2])
cube([radius+.5,radius+.5,height+.5]);
translate([radius,radius,height/2])
cylinder(height,radius,radius,true);
}
translate([0,yy,0])
rotate(270)
difference(){
translate([-.5,-.5,-.2])
cube([radius+.5,radius+.5,height+.5]);
translate([radius,radius,height/2])
cylinder(height,radius,radius,true);
}
}
}

Related

How to reposition a circle to be outside of circumference of two other circles?

This is a question for Unity people or Math geniuses.
I'm making a game where I have a circle object that I can move, but I don't want it to intersect or go into other (static) circles in the world (Physics system isn't good enough in Unity to simply use that, btw).
It's in 3D world, but the circles only ever move on 2 axis.
I was able to get this working perfectly if circle hits only 1 other circle, but not 2 or more.
FYI: All circles are the same size.
Here's my working formula for 1 circle to move it to the edge of the colliding circle if intersecting:
newPosition = PositionOfStaticCircleThatWasJustIntersected + ((positionCircleWasMovedTo - PositionOfStaticCircleThatWasJustIntersected).normalized * circleSize);
But I can't figure out a formula if the moving circle hits 2 (or more) static circles at the same time.
One of the things that confuse me the most is the direction issue depending on how all the circles are positioned and what direction the moving circle is coming from.
Here's an example image of what I'm trying to do.
Since we're operating in a 2D space, let's approach this with some geometry. Taking a close look at your desired outcome, a particular shape become apparent:
There's a triangle here! And since all circles are the same radius, we know even more: this is an isosceles triangle, where two sides are the same length. With that information in hand, the problem basically boils down to:
We know what d is, since it's the distance between the two circles being collided with. And we know what a is, since it's the radius of all the circles. With that information, we can figure out where to place the moved circle. We need to move it d/2 between the two circles (since the point will be equidistant between them), and h away from them.
Calculating the height h is straightforward, since this is a right-angle triangle. According to the Pythagorean theorem:
// a^2 + b^2 = c^2, or rewritten as:
// a = root(c^2 - b^2)
float h = Mathf.Sqrt(Mathf.Pow(2 * a, 2) - Mathf.Pow(d / 2, 2))
Now need to turn these scalar quantities into vectors within our game space. For the vector between the two circles, that's easy:
Vector3 betweenVector = circle2Position - circle1Position
But what about the height vector along the h direction? Well, since all movement is on 2D space, find a direction that your circles don't move along and use it to get the cross product (the perpendicular vector) with the betweenVector using Vector3.Cross(). For
example, if the circles only move laterally:
Vector3 heightVector = Vector3.Cross(betweenVector, Vector3.up)
Bringing this all together, you might have a method like:
Vector3 GetNewPosition(Vector3 movingCirclePosition, Vector3 circle1Position,
Vector3 circle2Position, float radius)
{
float halfDistance = Vector3.Distance(circle1Position, circle2Position) / 2;
float height = Mathf.Sqrt(Mathf.Pow(2 * radius, 2) - Mathf.Pow(halfDistance, 2));
Vector3 betweenVector = circle2Position - circle1Position;
Vector3 heightVector = Vector3.Cross(betweenVector, Vector3.up);
// Two possible positions, on either side of betweenVector
Vector3 candidatePosition1 = circle1Position
+ betweenVector.normalized * halfDistance
+ heightVector.normalized * height;
Vector3 candidatePosition2 = circle1Position
+ betweenVector.normalized * halfDistance
- heightVector.normalized * height;
// Absent any other information, the closer position will be assumed as correct
float distToCandidate1 = Vector3.Distance(movingCirclePosition, candidatePosition1);
float distToCandidate2 = Vector3.Distance(movingCirclePosition, candidatePosition2);
if (distToCandidate1 < distToCandidate2){
return candidatePosition1;
}
else{
return candidatePosition2;
}
}

Dynamically generate object with shape of cylinder segment

Is it possible to generate an object shaped like this dynamically in unity?
I need the angle of the object to be able to change from a thin sliver to a full donut-like part, so modelling each possible version would be very time-consuming and hard to use.
You can programmatically generate meshes like comments say.
However, if the angle is dynamic i.e. changes often, doing so will waste CPU time and GPU bandwidth best spent on something else. A better option, model (or generate programmatically at startup) 180° or 270° shape, and write a vertex shader to roll up or roll down the shape around center.
To keep things simple, model your shape so the cylinder axis is Z, center XY is {0, 0} (Z doesn’t matter), the opening of the shape is directed towards -X, and the shape is symmetrical around Y=0 plane. I’m talking about object’s local coordinates, you can then position/rotate the model however you want.
Here’s vertex shader code (untested):
float len = length( float2( position.x, position.y ) );
float newAngle = atan2( position.y, position.x ) * rollFactor;
float3 newPosition = float3( len * cos( newAngle ), len * sin( newAngle ), position.z );
output.position = UnityObjectToClipPos( newPosition );
This way, you only need to update a single constant variable rollFactor to transform your shape into any angle. If the initial shape is 270°, set the constant to 1.33334 to roll up into a full donut, 1.0 to keep it at 270°, 0.33333 to roll down to 90° like on your screenshot, etc.

How to convert leaflet circle radius to nautical miles?

I have a leaflet circle with a radius, how do I convert the radius to nautical miles in javascript?
Note that for Leaflet's circle, the radius corresponds to meters (see L.circle). Then you can convert meters directly into nautical miles.
Note that L.circle is just an approximation:
It's an approximation and starts to diverge from a real circle closer to poles (due to projection distortion).
To draw a gedetically accurate circle, use something like Leaflet.Geodesy.
A quick JS function to convert meters to miles -
function metersToMiles(meters){
if (isNaN(meters)){
console.log("meters is not a number");
return 0;
}
return meters/1852;
}
Also a good thing to note is that the draw tools for leaflet.draw have a way to draw a circle with nautical miles as the unit of measure -
draw: {
circle: {
metric: false,
feet: false,
nautic: true
}
}
leaflet draw documentation link
It looks like there is a "readableDistance" on the L.GeometryUtil object too but I only used the code above so I am not sure how that would work.

position of a point in circle's arc matlab

i have a circle and a point on it in matlab :
center = [Xc1 Yc1];
circle = [center 150];
point=[ 54.8355 116.6433]
I want to partition this circle into 8 arc and find out which arc is this point in ? how can i do this in matlab?
(i used this code to draw circle :
http://www.mathworks.com/matlabcentral/fileexchange/7844-geom2d/content/geom2d/geom2d/intersectLineCircle.m)
Dividing a circle into 8 arcs can be stated another way: cutting a pie into 8 pieces. These pie pieces each have an angle of 360/8 = 45 degrees. You can then think of the circle being broken up into these angle ranges (in degrees):
[0,45)
[45,90)
[90,135)
[135,180)
[180,225)
[225,270)
[270,315)
[315,0)
You'll have to then calculate the angle between the line that is made when you connect your point to the center of the circle and the x-axis. When you calculate this angle, you'll see which 'angle bin' it belongs to.

Car turning circle and moving the sprite

I would like to use Cocos2d on the iPhone to draw a 2D car and make it steer from left to right in a natural way.
Here is what I tried:
Calculate the angle of the wheels and just move it to the destination point where the wheels point to. But this creates a very unnatural feel. The car drifts half the time
After that I started some research on how to get a turning circle from a car, which meant that I needed a couple of constants like wheelbase and the width of the car.
After a lot of research, I created the following code:
float steerAngle = 30; // in degrees
float speed = 20;
float carWidth = 1.8f; // as in 1.8 meters
float wheelBase = 3.5f; // as in 3.5 meters
float x = (wheelBase / abs(tan(steerAngle)) + carWidth/ 2);
float wheelBaseHalf = wheelBase / 2;
float r = (float) sqrt(x * x + wheelBaseHalf * wheelBaseHalf);
float theta = speed * 1 / r;
if (steerAngle < 0.0f)
theta = theta * -1;
drawCircle(CGPointMake(carPosition.x - r, carPosition.y),
r, CC_DEGREES_TO_RADIANS(180), 50, NO);
The first couple of lines are my constants. carPosition is of the type CGPoint. After that I try to draw a circle which shows the turning circle of my car, but the circle it draws is far too small. I can just make my constants bigger, to make the circle bigger, but then I would still need to know how to move my sprite on this circle.
I tried following a .NET tutorial I found on the subject, but I can't really completely convert it because it uses Matrixes, which aren't supported by Cocoa.
Can someone give me a couple of pointers on how to start this? I have been looking for example code, but I can't find any.
EDIT After the comments given below
I corrected my constants, my wheelBase is now 50 (the sprite is 50px high), my carWidth is 30 (the sprite is 30px in width).
But now I have the problem, that when my car does it's first 'tick', the rotation is correct (and also the placement), but after that the calculations seem wrong.
The middle of the turning circle is moved instead of kept at it's original position. What I need (I think) is that at each angle of the car I need to recalculate the original centre of the turning circle. I would think this is easy, because I have the radius and the turning angle, but I can't seem to figure out how to keep the car moving in a nice circle.
Any more pointers?
You have the right idea. The constants are the problem in this case. You need to specify wheelBase and carWidth in units that match your view size. For example, if the image of your car on the screen has a wheel base of 30 pixels, you would use 30 for the WheelBase variable.
This explains why your on-screen circles are too small. Cocoa is trying to draw circles for a tiny little car which is only 1.8 pixels wide!
Now, for the matter of moving your car along the circle:
The theta variable you calculate in the code above is a rotational speed, which is what you would use to move the car around the center point of that circle:
Let's assume that your speed variable is in pixels per second, to make the calculations easier. With that assumption in place, you would simply execute the following code once every second:
// calculate the new position of the car
newCarPosition.x = (carPosition.x - r) + r*cos(theta);
newCarPosition.y = carPosition.y + r*sin(theta);
// rotate the car appropriately (pseudo-code)
[car rotateByAngle:theta];
Note: I'm not sure what the correct method is to rotate your car's image, so I just used rotateByAngle: to get the point across. I hope it helps!
update (after comments):
I hadn't thought about the center of the turning circle moving with the car. The original code doesn't take into account the angle that the car is already rotated to. I would change it as follows:
...
if (steerAngle < 0.0f)
theta = theta * -1;
// calculate the center of the turning circle,
// taking int account the rotation of the car
circleCenter.x = carPosition.x - r*cos(carAngle);
circleCenter.y = carPosition.y + r*sin(carAngle);
// draw the turning circle
drawCircle(circleCenter, r, CC_DEGREES_TO_RADIANS(180), 50, NO);
// calculate the new position of the car
newCarPosition.x = circleCenter.x + r*cos(theta);
newCarPosition.y = circleCenter.y + r*sin(theta);
// rotate the car appropriately (pseudo-code)
[car rotateByAngle:theta];
carAngle = carAngle + theta;
This should keep the center of the turning circle at the appropriate point, even if the car has been rotated.