Hi I need help with this question (triangle at mouse position)

Write a program to draw a triangle() with its center at the mouse pointer. The triangle should not change size or rotate as it moves. Give each triangle a different fill() colour. Try using a different mouse cursor.

1 Like

Hi @Computerscience,

what exactly is your problem on creating a solution for it ?

Here are some resources to look into …

Hope that helps you finding a solution…

— mnse

You need a sketch with setup and draw

You can say if (mousePressed) triangle (…

Use mouseX and mouseY to draw triangle

Show your own code and ask a specific question

Hello @Computerscience,

All the resources (tutorials, examples, references, etc.) you need are here:

Once you understand the basics build on that.

Look these up in the references once you have explored the basics:

  • createShape()



Hi, ComputerScience.

I learnt to program a ZX Spectrum when I was 12, 3 years before I studied trigonometry, so I am taking into consideration you could be in the same case.

Just in case your problem is about drawing a triangle with known centre, and you NEVER before studied trigonometry:

You can use sin() and cos() to make a equilateral triangle of known centre.

cy+sin(angle)*r, cx+cos(angle)*r is always a point in the limit of a circle with centre (cx,cy) and radius r.

An equilateral triangle has their angles separated 60 degrees each, this makes PI/3 radians (computers use radians instead of degrees).

I am surprised none gave help on the “triangle with known centre” issue. Triangles can have many kinds of “centre”, and only equilateral triangles have all “centres” in the same point. So if, by chance, your teacher wanted a square triangle with baricenter at mousex, mousey, the solution would be completely different.