Drawing a radius in D3 - d3.js

Apologies if this is painfully simple, I have tried searching for a solution.
In D3, I can perform an arc by defining this:
var ringBannerCcwArc = d3.svg.arc()
.innerRadius(420)
.outerRadius(470)
.startAngle(0)
.endAngle(function(t) {return t * -1 * 2 * Math.PI / 6; });
and then defining it in the DOM here:
labels.append("path")
.attr("id", "ring-banner");
And then at the appropriate time I can do:
labels.transition.select("#ring-banner").style("fill", "red")
.attrTween("d", function() { return ringBannerCcwArc });
And that will produce a red coloured arcing "label" starting at 0 and stopping at whatever
t * -1 * 2 * Math.PI / 6
produces as an angle (yes, 60 degress, but I intend for it to be a variable result).
What I would like to do is simply create a line that extends outward from this banner on the radius of the "endAngle" (so that I can build a dynamically driven display).
Something like this image:
My goal is to dynamically attach data to this and employ the amazingness of D3 to it. So once I understand how to draw the above solution, I would then like to know the resulting coordinates of the end line (x2,y2). The line itself can be a set length (say, 50) but how would I determine it's position so that I could attach a final area to it?
Something like this image:
Again, I apologize if this seems obvious. Thanks for any help.
EDIT:
The original question was regarding my use of D3 - creating a ringBannerArc - and then wanting to tap into the coordinate system. So, as the two respondents state, it is a basic trig problem but that's not really the question.
In the end, the answer is that d3 does not do what I was asking. But it can easily perform the solution. If, like me, you are struggling with implementing d3 and understanding it's very unique (and extremely powerful) approach to data visualization then you might find these links helpful. Thanks to the guys over at the d3 Google Group.
Helpful contributors:
Ian Johnson:
First you want to know how to draw a line from one point to another. That's the end result you want and if you can't do it for arbitrary lines you can't do it for the line you want. so lets start there:
http://tributary.io/inlet/4229462/ The second part is calculating the point on the circle you want to draw. But before you do that, you should set things up so you can verify easily where that point is. so lets draw the overall circle, and then draw a point we can use:
http://tributary.io/inlet/4229477/ Now lets try to place that point at some point on the circle based on an input radius using trig:
http://tributary.io/inlet/4229496/ once we can control that point, we come full circle ;) and move the line http://tributary.io/inlet/4229500/
Chris Viau: Wrapped it up in a nice helper function: http://jsfiddle.net/christopheviau/YPAYz/ Suggested a great book for beginners written by Scott Murray: http://ofps.oreilly.com/titles/9781449339739/index.html
Scott Murray: Makes a wonderful reference to a white paper written by the d3 authors - for those of us who like to understand the nuts and bolts: http://vis.stanford.edu/files/2011-D3-InfoVis.pdf

This is essentially a basic trigonometry question.
For a circle, if the angles start from vertical and go clockwise, and your coordinates are normal screen coordinates,
x = cx + sin(angle) * r
y = cy + cos(angle) * r
From these, you can then compute either line simply.

First you want to know how to draw a line from one point to another. That's the end result you want and if you can't do it for arbitrary lines you can't do it for the line you want.
The second part is calculating the point on the circle you want to draw. But before you do that, you should set things up so you can verify easily where that point is. so lets draw the overall circle, and then draw a point we can use:
http://tributary.io/inlet/4229477/
Now lets try to place that point at some point on the circle based on an input radius using trig, once we can control that point, we come full circle ;) and move the line
http://tributary.io/inlet/4229500/

Related

object instances created on xformed and rotated grids

In the fiddle below, I've drawn circles at certain points along a recursive tree structure.
https://jsfiddle.net/ypbprzzv/4/
The tree structure itself is a simplified version of the one found here:
https://processing.org/examples/tree.html
What if instead of drawing circles at (0, -h) on transformed and rotated grids, which is where they're being drawn in the fiddle, I wanted to hang pendulums which would hang in the unrotated y direction (down). If the pendulums were instances of an object class, it would be easy to add a new instance instead of (or in addition to) drawing the circle.
void branch(float h) {
h *= 0.6;
if (h > 10) {
pushMatrix();
rotate(a);
line(0, 0, 0, -h);
fill(0, 175, 0, 100);
if (h < 50) {
// I could add a new pendulum here, at (0, -h)
ellipse(0, -h, 5, 5);
}
translate(0, -h);
branch(h);
popMatrix();
} // closing the if statement
} // closing branch function
I have already tried this but because I wanted to keep the code very brief, I have not included it. The pendulums do indeed hang, but in wacky directions, since when I create these instances, the whole grid is xformed and rotated (which needs to be the case to simplify the drawing the tree or other interesting structures).
And suppose I want to make these pendulums sensitive to user interactions. The objects' frames of reference are different from the users'.
So I'll try to summarize the question:
Is it possible to create instances of objects on a transformed and rotated grid, but have that object behave in a prescribed way in relation to the unrotated grid?
Would it be helpful to provide a fiddle including the pendulums?
It's a little bit hard to help with general "how do I do this" or "is it possible" questions like this. The reason that it's hard to answer "is it possible" questions is the answer is almost always yes, it's possible. Similarly, "how do I do this" questions always have about a million possible answers, and there isn't any single best way to approach a problem. The "right" answer is really more dependent on how you think about the problem than anything else. But I'll try to help in a general sense.
Is it possible to create instances of objects on a transformed and rotated grid, but have that object behave in a prescribed way in relation to the unrotated grid?
Yes, it's possible.
There are a number of ways you might approach this:
You might keep track of the current state (current rotation) and then undo that when you draw the pendulum. For example, if you're rotated to 90 degrees, you'd simply rotate by -90 degrees before drawing the pendulum.
You could maybe use the screenX() and screenY() functions to get the screen location of a transformed point. More info can be found in the reference.
You could store positions as you recursively draw the tree, and then after the tree is drawn, you could loop over those points to draw the pendulums.
These are just what I could think of right now, and there are probably more ways to approach it. Again, which approach you choose really depends on how this stuff fits into your brain, which is pretty hard to help you with.
But if you want my two cents: I personally find it hard to "think in transformations" to do stuff like what you're describing. Instead, if I were you, I would refactor the code so it no longer relies on translations and rotations. I'd use basic trig (the sin() and cos() functions) to draw everything. Then you'd already be in screen coordinates, so drawing your pendulums would be much easier.
But again, which approach you take really depends on how you think about things.

Game Maker - Touch Event

I´m making my first game in Game Maker.
In the game i need to the user to draw a figure, for example a rectangle, and the game has to recognize the figure. How can i do this?
Thanks!
Well, that is a pretty complex task. To simplify it, you could ask him to place a succession of points, using the mouse coordinates in the click event, and automatically connect them with lines. If you store every point in the same ds_list structure, you will be able to check conditions of angle, distance, etc. This way, you can determine the shape. May I ask why you want to do this ?
The way I would solve this problem is pretty simple. I would create a few variables for each point when someone clicked on one of the points it would equal true. and wait for the player to click on the next point. If the player clicked on the next point i would call in a sprite as a line using image_angle to line both points up and wait for the player to click the next point.
Next I would have a step event waiting to see if all points were clicked and when they were then to either draw a triangle at those coordinates or place an sprite at the correct coordinates to fill in the triangle.
Another way you could do it would be to decide what those points would be and check against mouse_x, and mouse_y to see if that was a point and if it was then do as above. There are many ways to solve this problem. Just keep trying you will find one that works for your skill level and what you want to do.
You need to use draw_rectangle(x1, y1, x2, y2, outline) function. As for recognition of the figure, use point_in_rectangle(px, py, x1, y1, x2, y2).
I'm just wondering around with ideas cause i can't code right now. But listen to this, i think this could work.
We suppose that the user must keep his finger on touchscreen or an event is triggered and all data from the touch event is cleaned.
I assume that in future you could need to recognize other simple geometrical figures too.
1 : Set a fixed amount of pixels of movement defined dependent on the viewport dimension (i'll call this constant MOV from now on), for every MOV you store in a buffer (pointsBuf) the coordinates of the point where the finger is.
2 : Everytime a point is stored you calculate the average of either X and Y coordinates for every point. (Hold the previous average and a counter to reduce time complexity). Comparing them we now can know the direction and versus of the line. Store them in a 2D buffer (dirVerBuf).
3 : If a point is "drastically" different from the most plain average between the X and Y coordinates we can assume that the finger changed direction. This is where the test part of MOV comes critical, we must assure to calculate an angle now. Since only a Parkinsoned user would make really distorted lines we can assume at 95% that we're safe to take the 2nd point that didn't changed the average of the coordinate as vertex and let's say the last and the 2nd point before vertex to calculate the angle. You have now one angle. Test the best error margin of the user to find if the angle is about to be a 90, 60, 45, ecc.. degrees angle. Store in a new buffer (angBuf)
4 : Delete the values from pointsBuf and repeat step 2 and 3 until the user's finger leaves the screen.
5 : if four of the angles are of 90 degrees, the 4 versus and two of the directions are different, the last point is somewhat near (depending from MOV) the first angle stored and the two X lines and the Y lines are somewhat equal, but of different length between them, then you can connect the 4 angles using the four best values next to the 4 coordinates to make perfect rectangular shape.
It's late and i could have forgotten something, but with this method i think you could even figure out a triangle, a circle, ecc..
With just some edit and confronting.
EDIT: If you are really lazy you could instead use a much more space complexity heavy strategy. Just create a grid of rectangles or even triangles of a fixed dimension and check which one the finger has touched, connect their centers after you'have figured out the shape, obviously ignoring the "touched for mistake" ones. This would be extremely easy to draw even circles using the native functions. Gg.

How to make a hollow cylinder in SketchUp using the Ruby API

What I need to accomplish is basically a cylinder whose walls have zero thickness. Now if you need to understand better what I mean by this, imagine manually drawing a circle, then using the pushpull tool to make it a cylinder, after which you delete the top and bottom faces. At first, I used the method suggested in this post:
Punching a hole through a cylinder using Sketchup Ruby API
where the outer and inner radii had a difference of about 1e-02 meters, but now I realize it actually has to be an infinitesimal thickness, one where no matter how far you zoom-in, all you see is a line.
I went about trying to accomplish this with the following basic snippet of code:
entities = Sketchup.active_model.active_entities
circle = entities.add_circle(Geom::Point3d.new(0,0,0), Geom::Vector3d.new(0,0,1), 20)
face = entities.add_face(circle)
face.pushpull(-10, true)
# now from here, it can be either pushpulled downwards by the same amount (10 in this case
# leaving only a bottom face)
# or the entity 'face' can be erased (leaving only a top face) as in the following
entities.erase_entities(face)
so my question is, how do I remove both faces so as to leave only the cylinder?
Thank you.
I found the answer, the solution is to do both. That is, pushpull inwards the same amount, then delete the face with: entities.erase_entities(face).
entities = Sketchup.active_model.active_entities
circle = entities.add_circle(Geom::Point3d.new(0,0,0), Geom::Vector3d.new(0,0,1), 20)
face = entities.add_face(circle)
face.pushpull(-10, true)
face.pushpull(10, true)
entities.erase_entities(face)

What does cluster.size do in D3JS?

I am trying to create a graph based on Mike Bostock's Heirarchical Edge Bundling(here is the gist). I need to make my JSON look as readme-flare-imports.json looks, but I can't figure out what "size" is. I read the API and it didn't seem to help me. Also, it will be a dynamic JSON file based on a mySQL database, so I won't be able to set the size myself. Is anybody able to clear things up for me as to what it is or how I may be able to determine what the size should be? Thank you in advance!
cluster.size determines how large of an area the cluster will take up. You pass values to it like so
// The angle
var x = 360;
// The radius
var y = window.height / 2;
cluster.size([x, y])
x will determine how much of a circle the cluster will use to branch out children. A value of 360 will use the entire circle to display all values. A value of 180 will only use half the circle to branch out values.
y will determine how wide the circle will become in any single direction, i.e., the radius of the circle.
In the Heirarchical Edge Bundling example, I believe the size attribute in the json file is ignored as I could not find anything in the code that cared about it.

circle-circle collision problem

I have a problem with circle-circle collision detection.I used the following algorithm
func collision(id,other.id)
{
var vaP1,vaP2,dis,va1,vb1,va2,vb2,vp1,vp2,dx,dy,dt;
if (id!=other.id)
{
dx=other.x-x;
dy=other.y-y;
dis=sqrt(sqr(dx)+sqr(dy));
if dis<=radius+other.radius
{
//normalize
dx/=dis;
dy/=dis;
//calculate the component of velocity in the direction
vp1=hspeed*dx+vspeed*dy;
vp2=other.hspeed*dx+other.vspeed*dy;
if (vp1-vp2)!=0
{
dt=(radius+other.radius-dis)/(vp1-vp2);
//move the balls back so they just touch
x-=hspeed*dt;
y-=vspeed*dt;
other.x-=other.hspeed*dt;
other.y-=other.vspeed*dt;
//projection of the velocities in these axes
va1=(hspeed*dx+vspeed*dy);
vb1=(vspeed*dx-hspeed*dy);
va2=(other.hspeed*dx+other.vspeed*dy);
vb2=(other.vspeed*dx-other.hspeed*dy);
//new velocities in these axes. take into account the mass of each ball.
vaP1=(va1+bounce*(va2-va1))/(1+mass/other.mass);
vaP2=(va2+other.bounce*(va1-va2))/(1+other.mass/mass);
hspeed=vaP1*dx-vb1*dy;
vspeed=vaP1*dy+vb1*dx;
other.hspeed=vaP2*dx-vb2*dy;
other.vspeed=vaP2*dy+vb2*dx;
//we moved the balls back in time, so we need to move them forward
x+=hspeed*dt;
y+=vspeed*dt;
other.x+=other.hspeed*dt;
other.y+=other.vspeed*dt;
}
}
}
x=ball 1 x-position
y=ball 1 y-position
other.x= ball 2 x position
other.y=ball 2 y position
this algorithm works well when i have a ball image of 40 x 40 pixel and ball center is (20,20) means image consists only ball.But the problem arises when image size is 80 x 80.and ball center position is (60,60),means ball is lower right corner with radius 20.
in this case there are multiple collision occur,means the portion
x+=hspeed*dt;
y+=vspeed*dt;
other.x+=other.hspeed*dt;
other.y+=other.vspeed*dt;
unable to seperate the ball /velocity does not change according to collision.
I have changed the value of x which is the center of image 40,40 to 60,60 center of ball adding 20.but the result is same .Can any one tell me what is the problem.I think algorithm is correct because it works nicely in all other case and lots of people used this algorithm.problem is changing position from image center to ball center.what correction should i do for this??? or any idea.if someone want to help plz give me e-mail address so that i can send my full project.
I didnt have the mental power to digest your entire question, but here is my 2 cents on how to solve your problem
1) The simplest way to detect a circle collision with another is to check if their distance is less than the radius of the combined circles. (i might be wrong with the math, so correct me if i am wrong)
Circle c1,c2;
float distance = DISTANCE(c1.center,c2.center);
if(distance < c1.radius + c2.radius)
{
// collision .. BOOOOOOM
}
2) Try to use accurate data types. Try not to convert floats to integers without checking overflow, underflow and decimal points. Better still, just use floats .
3) Write a log and trace through your values. See if there are any obvious maths errors .
4) Break down your code to its simplest portion. Try to remove all that velocity computation to get the simplest movements to help you debug.
I will not give you the answer that you are looking for and I am not sure someone else will. The amount of code that must be decyphered to get you the answer may not warrant the reward. What I would recommend is to losen the coupling in your algorithm. The function above is doing way too much work.
Ideally you would have a collision detection that concentrated only on the collision and not on advancing the balls. Something like function shown below and that would allow other developers to help you more easily if you still had a problem.
function(firstCircleCenterX, firstCircleCenterY, secondCircleCenterX, secondCircleCenterY, firstCircleRadius, secondCircleRadius)
{
...this code should concentrate on logic to determine collision
...use pythagoran theory to find distance between the two centers
...if the distance between the two centers is less than ((2*firstCircleRadius)+(2*secondCircleRadius) then you have a collision
...return true or false depending on collision
}

Resources