How to draw a star in P5.js? - p5.js

I'm trying to make a class "star" with a method inside of it that, at given coordinates and size, draws a star. Something like rect() or ellipse() functions.
How can make it?

The best advice I can give you is to get out a piece of paper and a pencil. Draw out the points of a star, and label their X and Y coordinates.
Then you can use something like the line() or beginShape() function to draw that on the screen using code.
You can find more info in the P5.js reference.

With the new update on p5 you can simply type star(coordinates) and then make the star.

Related

Define rotational anchor point in the center of a quad shape in p5js

I know that there is rectMode(CENTER) of both rectangles and ellipses, but what about other shapes like triangles and quad shapes?
It's going to be hard to write a long reply because the answer is no, there isn't a rectMode() for triangles or shapes you create using the quad() or vertex() functions.
You could use the translate() function to translate to the center of the shape, and then draw all of the points relative to that. That would also solve the question that I think you're trying to ask about then calling the rotate() function.

Drawing polygons using latitude/longitude values in D3

I have a set of polygons where the position of their vertices (corners) are defined as latitude/longitude values.
I want to use D3 to draw these. I don't want anything fancy (like a background containing the world map, etc). The only thing I want to do is to draw the polygons.
I have looked up on Google for a solution, but what I find is things like http://datamaps.github.io or https://github.com/mbostock/d3/wiki/Geo-Paths and they all show a background of the world map or the like which is what I don't want to do.
Suggestions are welcome.
In your particular case, using the geo functionality of D3 seems like overkill -- all you need to do is use the D3 scales to convert the coordinates to screen coordinates, i.e.
var scale = d3.scale.linear().domain([minCoord, maxCoord]).range([minScreen, maxScreen]);
Demo (based on provided fiddle) here.

How does one override "draw" in a CCNode?

I am creating a side scrolling game.
I compute all the points representing what a terrain should look like by doing the following:
The points representing the top of the hill are determined by using a sin function.
The bottom of the hill is just the bottom of the screen.
The left and right edges of the terrain, are the left and right edges of the screen, where the x coords are x=0, and x= screen width.
But I don't know how to draw it on screen, and "fill" it with some other texture. (A predetermined PNG image or something).
How does one override the draw method of a CCNode or CCSprite to accomplish this ?
In the example below I would use a square png image of stars, which I would like to repeat as I scroll the terrain from left to right.
Edit: In the tutorial below, they do all kinds of calculations and wrap a sprite around the hills. But I just want to do something simple, like fill the hills with a simple "noise" texture (no stripes etc), or a solid color. How can I do that?
: http://www.raywenderlich.com/32954/how-to-create-a-game-like-tiny-wings-with-cocos2d-2-x-part-1
EDIT: To clarify: I know one can override the draw method. But I don't know what code to put in it to accomplish the problem I described above.
Create a new class that subclasses CCNode or CCSprite, and add a draw method:
- (void) draw {
}
The draw method will automatically execute each frame. Put whatever "drawing" code you like within it.

LibGDX - The best way detect colission

I have an airplane. I use rectangle for bounding this airplane to detect collision and it works great. When the airplane begin falling down I rotate airplane's texture, but rectangle remains unchanged. I don't know how to rotate it. I need to rotate it with airplane's texture because my shell doesn't collide the airplane's tail and cabine.
How to rotate rectangle or perhaps create polygon shape to wrap all airplane? Any help will be appreciated!
#jellyfication's answer points to raycasting, but a different and also simple approach you could implement is the Separating Axis Theorem. The links below will show you in detail what the algorithm is about and how to implement it. They also have some interactive demos so you get the 'feel' for what the algorithm is doing.
http://www.metanetsoftware.com/technique/tutorialA.html
http://www.sevenson.com.au/actionscript/sat/
http://www.codezealot.org/archives/55 (this one has a lot of code)
http://gamedev.tutsplus.com/tutorials/implementation/collision-detection-with-the-separating-axis-theorem/
Good luck!
Use the polygon class to and draw your bounding Box.
Then within the polygon class there is a method to rotate.
Rotate and move the polygon with the plane.

polyline with gradient

Is there a way to draw a line along a curved path with a gradient that varies in a direction perpendicular to the direction of the line? I am using the GDI+ framework for my graphics.
The simple answer is no. You can create a GraphicsPath in order to describe what you would like to draw, using AddPoint/AddLine/AddBezier and so forth as needed to describe the complex path of what you want to draw. When you draw the path you can provide a Brush which can be something like LinearGradientBrush or RadialGradientBrush. Neither of those gradient brushes reacts to the actual path being drawn in the sense of changing direction as the drawing occurs. You have to specify the angles etc as constant for the entire gradient area.
One possible method you can use is to set the clip region of the Graphics object to be that of the line only. Then draw a Linear Gradient over the extremes of the line e.g.
GraphicsPath gp = new GraphicsPath();
gp.AddArc(); // etc...
graphics.SetClip( gp );
graphics.FillRectangle( myLinearGradientBrush, gp.GetBounds());
The above code might give you what you are looking for.

Resources