164k views
21 votes
Write a program that will draw a red circle on top of a blue square in the center of the canvas. The blue square should have sides of 100 pixels each and the red circle should have a diameter of 100 pixels

it's thru codehs and it's due tomorrow so

User Levi Cowan
by
3.7k points

2 Answers

5 votes

Final answer:

A program to draw a red circle on top of a blue square in the center of a canvas would require calculating the center position for both shapes using their dimensions and the canvas size, then using graphics functions to fill the shapes in the appropriate colors.

Step-by-step explanation:

Program to Draw a Red Circle on Top of a Blue Square

To accomplish the task of drawing a red circle on top of a blue square in the center of the canvas using a programming platform like CodeHS, one could use a language like JavaScript with a graphics library. Here's a sample code snippet:

var canvas = document.getElementById('myCanvas');
var context = canvas.getContext('2d');
var squareSize = 100;
var circleDiameter = 100;
// Draw blue square
context.fillStyle = 'blue';
context.fillRect((canvas.width - squareSize) / 2, (canvas.height - squareSize) / 2, squareSize, squareSize);
// Draw red circle
context.beginPath();
context.arc(canvas.width / 2, canvas.height / 2, circleDiameter / 2, 0, 2 * Math.PI, false);
context.fillStyle = 'red';
context.fill();

The blue square has sides of 100 pixels each, and the red circle has a diameter of 100 pixels. This is placed in the center of the canvas by calculating the starting points for the square and the circle's center.

Make sure to adjust the canvas element's size appropriately and include the script in an environment where it can run, such as an HTML page with a canvas element with the id 'myCanvas'.

User Friveraa
by
3.8k points
9 votes

Final answer:

To draw a red circle over a blue square on a canvas, use a graphics library to create the shapes and position them at the center of the canvas, ensuring the circle has a radius of 50 pixels (100 pixels diameter) and the square has sides of 100 pixels each.

Step-by-step explanation:

To draw a red circle on top of a blue square in the center of a canvas, you can use a graphics programming library, such as the one provided by CodeHS. Here is a simple program written in JavaScript that uses the CodeHS library for this purpose:

function start(){
// Draw blue square
var square = new Rectangle(100, 100);
square.setColor(Color.blue);
square.setPosition(getWidth()/2 - square.getWidth()/2, getHeight()/2 - square.getHeight()/2);
add(square);

// Draw red circle
var circle = new Circle(50);
circle.setColor(Color.red);
circle.setPosition(getWidth()/2, getHeight()/2);
add(circle);
}

This code first creates a blue square with a side length of 100 pixels, and then places it at the center of the canvas. After that, a red circle with a radius of 50 pixels (diameter 100 pixels) is drawn and placed at the same center point, overlapping the square.

User Vladislav Volynets
by
3.6k points