Make a program with percentage

Hello, I would like to create a code where we can move shield shapes on a warrior and know the percentage of the warrior covered, I unfortunately do not know how to do it, help me please

Can you describe more what you are trying to do? Are you dragging an outline of a shield with the mouse on top of an outline of a person, and calculating the amount of the person visible based on where you dragged the shield?

Are you putting on different shields, always held in the hand, or moving one shield around to different places on the body?

More details please!

One solution would be to use a mask() and count the number of visible pixels to find the exposed area.

Thank you for replying, I wish I could move different shield with the mouse on any part of the warrior’s body, and then know how many pixels belong to the warrior are covered by the different shields. I’m really not very good at this software so it’s a difficult task for me.

I recommend you start by making a sketch like this:

  1. it shows a circle (the warrior)
  2. a rectangle (the shield) is dragged around by the mouse – don’t even bother dropping it, just draw it where-ever the mouse is.

Why don’t you start by making that sketch and sharing it here in this thread – then we can explore how to pick-up / drop the shield, and how measure the overlap.

Ok, I’m doing this tomorrow and I’ll send it to you, thank you very much.

Here is my schema, I draw the basic plan for my animation.

Thanks for this drawing. It makes some things about your concept clear!

By a sketch, I also meant a sketch in Processing – for example, like this.

void draw(){
  background(0);
  fill(255,0,0);
  rect(25, 25, 50, 75);
  fill(0,0,255);
  ellipse(mouseX, mouseY, 50, 50);
}

37%20PM

Have you done any programming in Processing (or anything else) before, or is this your very first sketch?

Yes, my first program on this site and I do not have much computer knowledge, I did this skit to demonstrate my presentation topic for the French BAC.

!
Here’s what I already did, in my program I can only move the ellipse but I can not do the same for the other three shields.

Here’s what it gives after running the program

Hi @justin38

Thank youi. Please don’t post screenshots of your code. Instead, cut and paste your code into the forum, and highlight and format it with the </> editor button.

Forum members can’t run or debug your screenshot – and we shouldn’t have to type your whole program back in to get it to work!

I notice from your screenshot that you are using Python mode rather than Java mode, so I will move this conversation into that part of the forum. I also notice that you are using a combination of primitive shapes and vertex shapes.

def setup():
    size(700,500)
    fill(200,200,0)
X=100
Y=100
def draw():
    global X,Y
    background(255)
    fill(255,255,0)   
    beginShape()
    vertex(250,400)
    vertex(250,240)
    vertex(267,230)
    vertex(284,230)
    vertex(284,225)
    vertex(274,220)
    vertex(274,205)
    vertex(284,190)
    vertex(316,190)
    vertex(326,205)
    vertex(326,220)
    vertex(316,225)
    vertex(316,230)
    vertex(333,230)
    vertex(350,240)
    vertex(350,400)
    endShape(CLOSE)
#save("guerrier.tif")
    fill(150)
    ellipse(X,Y,100,160)
#crée bouclier
    fill(150)
    rect(50,250,100,160)
#crée bouclier
    fill(150)
    ellipse(500,100,130,130)
#crée bouclier
    fill(150)
    beginShape()
    vertex(500,250)
    vertex(560,250)
    vertex(580,280)
    vertex(580,380)
    vertex(560,410)
    vertex(500,410)
    vertex(480,380)
    vertex(480,280)
    endShape(CLOSE)

def mouseDragged():
    global X,Y
    X=mouseX
    Y=mouseY

Hi, actually I use python, will it still be you if you change me part of the forum?

I will still respond to Python questions, yes. Hopefully other Python users will also respond.

Now that you have a sketch

One solution would be to use a mask()

You could use a PGraphics (or PGraphics.mask()) and then use PGraphics.get to count the number of visible pixels to find the exposed area.

  1. Draw your warrior shape onto the PGraphics in white
  2. Draw the shield shape in black
  3. Count the number of white pixels on the PGraphics – this is your score.

Hello, I will try to do your technique and I will recontacterize if I can not do it or if I have a problem.

Here is a very simple example without PGraphics, written for Python mode.

The console prints the amount of warrior visible as a count of the white pixels and as a percentage of the white pixels. Move the shield (circle) over the warrior (rectangle) and watch the results change.

def setup():
  size(300, 200)
  noStroke()

def draw():
  background(0)
  fill(255)
  body_w = 100.0
  body_h = 150.0
  body_area = body_w * body_h
  rect(100, 50, 100, 150)
  fill(128)
  ellipse(mouseX, mouseY, 140, 140)
  body_exposed = count_color_pixels(color(255))
  body_percent_exposed = float(nfc(body_exposed / body_area, 2))
  print(body_exposed, body_percent_exposed)

def count_color_pixels(match_color):
  measure = 0
  loadPixels()
  for pix in pixels:
    if pix == match_color:
      measure = measure + 1
  return measure

In order to integrate this into a sketch with a different display (such as graphics), draw the simple measurement colors onto a PGraphics, and draw your display shapes onto the main canvas.

Hello, sorry for my absence. I did not understand everything in your program and when I want to execute it, it signals me an error in this line : body_percent_exposed = float (nfc(body_exposed / body_area, 2)). With writing:
“invalid litteral for float : 1.00”

Strange. The sketch works as given. Perhaps you made a copy and paste error?

My best guess – perhaps you have non-English language settings that write render floats with a comma, and that is causing an error for the parser? I really don’t know. This works in Python mode in PDE 3.4

Does this work?

print(1.00)
print('1.00')
print(float(1.00))
print(float('1.00'))

1.0
1.00
1.0
1.0