Randomly moving circles in processing - functional-programming

I have figured out how to make a circle move randomly, but now I want there to be 100 circles, but can't figure out how to add them, HELP!
float my_num = 10;
float n = random(5, 60);
void setup() {
size(500, 500);
background(0);
fill(255);
noStroke();
smooth();
rectMode(CENTER);
}
void draw() {
background(0);
translate(width * noise(my_num + 80), height * noise(my_num + 100));
rotate(10 * noise(my_num + 40));
ellipse(n, n, n, n);
my_num = my_num + 0.02;
}

Sounds like you should create a Circle class that contains the position, size, color, etc of a circle.
Then create an ArrayList or array that contains 100 Circles, and loop through them whenever you want to update or draw those circles.
Recommended reading: http://staticvoidgames.com/tutorials/objects/fireworks

Related

JavaFX rotate rectangle about center?

I'm trying rotate a rectangle around its center. The rotation is being drawn to a canvas using the GraphicsContext ie gc. Here is my draw code.
gc.save();
gc.translate(center.x, center.y);
gc.rotate(this.angle);
gc.strokeRect(0,0, this.width, this.height);
gc.restore();
This moves the rectangle to its center but then it rotates the rectangle around its top left point. I tried subtracting half the length and width of the sides but that just made it fly all over the place. I suck at math maybe someone here who is better can show me what I'm doing wrong.
I also have stored all four points (corners) of the rectangle if that information is needed.
Thanks,
Joe
A rotation around a specified point needs to be composed from translate transformations and rotations around the origin as follows:
Use translation to move the center of the rotation to the origin.
rotate around the origin
use inverse translation to the first translation
The third part is missing from your code.
Example
#Override
public void start(Stage primaryStage) throws Exception {
Canvas canvas = new Canvas(400, 400);
double x = 50;
double y = 100;
double width = 100;
double height = 200;
GraphicsContext gc = canvas.getGraphicsContext2D();
double rotationCenterX = (x + width) / 2;
double rotationCenterY = (y + height) / 2;
gc.save();
gc.translate(rotationCenterX, rotationCenterY);
gc.rotate(45);
gc.translate(-rotationCenterX, -rotationCenterY);
gc.fillRect(0, 0, width, height);
gc.restore();
Scene scene = new Scene(new Group(canvas));
primaryStage.setScene(scene);
primaryStage.show();
}
You could also simply use a Rotate with a specified pivot to achieve the desired effect:
#Override
public void start(Stage primaryStage) throws Exception {
Canvas canvas = new Canvas(400, 400);
double x = 50;
double y = 100;
double width = 100;
double height = 200;
GraphicsContext gc = canvas.getGraphicsContext2D();
double rotationCenterX = (x + width) / 2;
double rotationCenterY = (y + height) / 2;
gc.save();
gc.transform(new Affine(new Rotate(45, rotationCenterX, rotationCenterY)));
gc.fillRect(0, 0, width, height);
gc.restore();
Scene scene = new Scene(new Group(canvas));
primaryStage.setScene(scene);
primaryStage.show();
}
It's just so simple, like below.
Rectangle rect = new Reactangle(20, 20, 100, 50);
rect.setRotate(30); //rotate the rectangle around its center by 30 degrees.

Bounding rect to the complex form?

The subjects of our project is making a program who simulate a Fusion.
We have some problem with the colliding with our classe Fusion. We want to make a shape complex for our colliding.
printScreenFusionProgramm
Our shape is two circle near each other and we dont want to have a bounding rect but shape "complex"...
this is our Fusion class
Fusion::Fusion(int x, int y)
{
this->setPos(x, y);
}
void Fusion::shape(){
//...
}
void Fusion::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
//set the color
QBrush brushColor(QColor(Qt::blue));
painter->setBrush(brushColor);
painter->setPen(QColor(Qt::blue));
painter->drawEllipse(0,0,40,40);
painter->drawEllipse(-20,-20,40,40);
}
void Fusion::doCollision()
{
// get a new position
// change the angle with randomness
if(qrand() %1)
{
setRotation(rotation() + (180 + (qrand() % 10)));
}
else
{
setRotation(rotation() + (180 + (qrand() % -10)));
}
// check if the new position is in bounds
QPointF newPoint = mapToParent(-(boundingRect().width()), -(boundingRect().width() + 2));
if(!scene()->sceneRect().contains((newPoint)))
{
// move back in bounds
newPoint = mapToParent(0,0);
}
else
{
// set the new position
setPos(newPoint);
}
}
void Fusion::advance(int step)
{
if(!step) return;
if(!scene()->collidingItems(this).isEmpty())
{
doCollision();
}
setPos(mapToParent(0, -1));
}
You need to reimplement the "shape" method for your graphics items to return the actual shape of your object. You can return any shape you want in a QPainterPath, and Qt will use that for collision detection.

Panning camera so object is at edge of screen

I'm trying to write a formula to pan the camera enough so that the center point of an object is just visible at the edge of the screen.
So, in other words if the object is out of view to the right, I would change the x and y position of the camera, so that the object is just at the right edge of the screen (without changing camera angle or z co-ordinate).
Can anyone give me any hints how to do this?
I figured out a solution that serves my purpose, but the only way I could do it was by modifying the camera height (which is not exactly what I wanted):
// note: pos is the center of the object that is to appear at edge of screen
// m_position is the 3d position of the camera
// m_plane[] is array of left, right, etc. planes of camera fustrum
void Camera::PanTo(const Vector3D& pos)
{
int n;
Vector3D vectorNearest;
for (n = 0; n < NUM_PLANES; n++)
{
if (m_plane[n].GetDistance(pos) < 0)
{
m_plane[n].Normalize();
vectorNearest += m_plane[n].GetVectorNearest(pos);
}
}
m_posDesire.m[IX_X] = m_position.m[IX_X] + vectorNearest.m[IX_X];
m_posDesire.m[IX_Y] = m_position.m[IX_Y] + vectorNearest.m[IX_Y];
m_posDesire.m[IX_Z] = m_position.m[IX_Z] + vectorNearest.m[IX_Z];
}
// This is the definition of the Plane class:
class Plane
{
public:
void Normalize()
{
float lenInv = 1.0f/sqrtf(m_a*m_a + m_b*m_b + m_c*m_c);
m_a *= lenInv;
m_b *= lenInv;
m_c *= lenInv;
m_d *= lenInv;
}
float GetDistance(const Vector3D& pos) const
{
return m_a*pos.m[IX_X] + m_b*pos.m[IX_Y] +
m_c*pos.m[IX_Z] + m_d;
}
Vector3D GetVectorNearest(const Vector3D& pos) const
{
Vector3D normal(m_a, m_b, m_c);
float posDotNormal = pos.dotProduct(normal);
Vector3D nearest = normal*(m_d+posDotNormal);
return nearest;
}
float m_a, m_b, m_c, m_d;
};

Set zoom out limit for a picture in QGraphicsView

I am zooming an image on a label using QGraphicsView. But when I zoom out I want to set a specific limit for the zoom out. I am using the following code
scene = new QGraphicsScene(this);
view = new QGraphicsView(label);
QPixmap pix("/root/Image);
Scene->addPixmap(pixmap);
view->setScene(scene);
view->setDragMode(QGraphicsView::scrollHandDrag);
In the slot of wheel event
void MainWindow::wheelEvent(QWheelEvent *ZoomEvent)
{
view->setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
double scaleFactor = 1.15;
if(ZoomEvent->delta() >0)
{
view->scale(scaleFactor,scaleFactor);
}
else
{
view->scale(1/scaleFactor,1/scaleFactor);
}
}
I want that the image should not zoom out after a certain extent. What should I do?
I tried setting the minimum size for QGraphicsView but that didn't help.
Thank You :)
Maybe something like this would help:
void MainWindow::wheelEvent(QWheelEvent *ZoomEvent)
{
view->setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
static const double scaleFactor = 1.15;
static double currentScale = 1.0; // stores the current scale value.
static const double scaleMin = .1; // defines the min scale limit.
if(ZoomEvent->delta() > 0) {
view->scale(scaleFactor, scaleFactor);
currentScale *= scaleFactor;
} else if (currentScale > scaleMin) {
view->scale(1 / scaleFactor, 1 / scaleFactor);
currentScale /= scaleFactor;
}
}
The idea, as you can see, is caching the current scale factor and do not zoom out in case of it smaller than some limit.
Here is one more implementation idea in which the current zoom factor is obtained from the transformation matrix for the view:
void View::wheelEvent(QWheelEvent *event) {
const qreal detail = QStyleOptionGraphicsItem::levelOfDetailFromTransform(transform());
const qreal factor = 1.1;
if ((detail < 10) && (event->angleDelta().y() > 0)) scale(factor, factor);
if ((detail > 0.1) && (event->angleDelta().y() < 0)) scale((1 / factor), (1 / factor));
}
Another way to solve the problem:
void GraphWidget::wheelEvent(QWheelEvent *event)
{
int scaleFactor = pow((double)2, - event->delta() / 240.0);
qreal factor = transform().scale(scaleFactor, scaleFactor).mapRect(QRectF(0, 0, 1, 1)).width();
if (factor < 0.5 || factor > 20)
return;
scale(scaleFactor, scaleFactor);
}

AS3: Math-related - Constructing Equilateral Triangle

I am trying to construct an equilateral triangle with the following code, but I am not seeing what I would expect to.
I would expect to see a triangle drawn to the stage, but I don't see one.
I think there must be something wrong in terms of the location of the tri sprite on the screen.
public class test_drawing_triangle extends Sprite
{
public function test_drawing_triangle()
{
var tri:Sprite = new Sprite;
var nextx:int;
var nexty:int;
var prevx:int;
var prevy:int;
var angle:Number;
var radius:Number;
var deg:int;
var i:int;
radius = 10;
// tri.x = stage.stageWidth / 2;
tri.y = 50;
tri.graphics.clear();
tri.graphics.beginFill(0x000000, 0.5);
tri.graphics.moveTo(0,0);
for(deg = 0; deg < 360; deg += 120)
{
angle = deg * Math.PI / 180;
nextx = Math.cos(angle) * radius;
nexty = Math.sin(angle) * radius;
tri.graphics.lineTo(nextx, nexty);
}
tri.graphics.endFill();
addChild(tri);
}
}
UPDATE:
I can now see the triangle but it is not filled in.
It seems to have the generally-correct shape, but I would expect for it be 3 sided, rather than 4.
If anyone could take a sec to compile this and look at what I am describing it would really help.
I'm not sure if I understand what you're trying to do, but if you want to have the circles at the corners of the triangle. than you need to change the
tri.graphics.drawCircle(0, 0, 2);
to
tri.graphics.drawCircle(nextx, nexty, 2);
drawCircle takes absolute x,y and doesn't care about the moveTo
EDIT - use this code in place of your loop
deg = 30;
angle = (deg * Math.PI) / 180.0;
nextx = Math.cos(angle) * radius;
nexty = Math.sin(angle) * radius;
(tri.graphics as Graphics).moveTo(nextx, nexty);
for(deg = 150; deg < 420; deg += 120)
{
angle = (deg * Math.PI) / 180.0;
nextx = Math.cos(angle) * radius;
nexty = Math.sin(angle) * radius;
(tri.graphics as Graphics).lineTo(nextx, nexty);
}
This code looks like its drawing three circles centered on the points of your triangle. If that's the case, then replace
tri.graphics.moveTo(nextx, nexty);
tri.graphics.drawCircle(0, 0, 2);
with
tri.graphics.lineTo(nextx, nexty);
If that's not the case, please specify what it is you are seeing so we might have a better idea of what's gone wrong.
Is there are particular reason why you are using angles to draw the triangle?
If I were to do this, I'd simply hard code a unit sized equilateral triangle (defined as three 3d points) once and then use scale and translation operations to put it in the correct size and position.

Resources