polygon edge position in maxscript - polygons

I want to get polygon edge positions 3Ds Max with maxscript. I have tried the following:
tmesh = snapshotAsMesh selection[1]
out_name = ((GetDir #export)+"/testmesh.dat")
out_file = createfile out_name
num_verts = tmesh.numverts
num_faces = tmesh.numfaces
format "% % %\n" num_verts num_faces to:out_file
for v = 1 to num_verts do
(
vert = getVert tmesh v
format "%," vert to:out_file
)
format "\n" to:out_file
for f = 1 to num_faces do
(
face = getFace tmesh f
format "%," face to:out_file
)
close out_file
delete tmesh
edit out_name
Output needs to be something like OBJ file format, but I want to have only edge connections showed in wire frame.
Sample output:
[0,0,0],[1,0,0],[0,1,0],[1,1,0]
[1,2],[2,3],[3,4],[4,1]
EDIT:
I could find invisible edges via this code.
for f = 1 to num_faces do
(
face = getFace tmesh f
edge1 = getEdgeVis tmesh f 1
edge2 = getEdgeVis tmesh f 2
edge3 = getEdgeVis tmesh f 3
format "%,[%,%,%]" face edge1 edge2 edge3 to:out_file
format "\n" to:out_file
)

You can get the vertices for an edge using meshop.GetVertsUsingEdge like this:
tmesh = snapshotAsMesh selection[1]
allverts = #()
for v = 1 to num_verts do
(
vert = getVert tmesh v
append allverts vert
)
print allverts
format "\n" --to:out_file
edges = tmesh.Edges
for ed in edges do
(
print ed
edverts = meshop.GetVertsUsingEdge tmesh ed
print "ed verts: "
for v in edverts do (
print allverts[v]
)
print "\n"
)
delete tmesh

Related

Using count() for OVERLAPPING CHARACTERS

My code is designed to count the the letters that appears in 2 separate names, including the overlapping characters. it should take two names (name = 1st name + last name) and count the number of times the letters "T R U E L O V E" show up.
My code is:
''' # 🚨 Don't change the code below 👇
print("Welcome to the Love Calculator!")
name1 = (input("What is your name? \n"))
name2 = input("What is their name? \n")
# 🚨 Don't change the code above 👆
#Write your code below this line 👇
names = name1 + name2
lowernames = names.lower()
t = lowernames.count("t")
r = lowernames.count("r")
u = lowernames.count("u")
e = lowernames.count("e")
l = lowernames.count("l")
o = lowernames.count("o")
v = lowernames.count("v")
e = lowernames.count("e")
true = t + r + u + e
love = l + o + v + e
love_score = int(str(true) + str(love))
print(love_score)
if (love_score < 10) or (love_score > 90):
print(f"your love score is {score} you fit together like montos and coke")
elif (love_score >= 45) and (love_score <= 50):
print(f"your love score is {love_score} you are alright together")
else:
print(f"your love score is {love_score}")
'''
The names I've entered in are:
Yonatan Allon (should give 2 O's and 2 L's)
Guy Hazan (one U)
I don't get why the score that comes up is 24 and not 41
Can someone please help me fix this?
Thanks in advance, Yonatan :)

Why does Fractal Formula "sin(z^2)-cos(z^2)+c" work with XaoS and not UltraFractal?

While using the real-time Fractal zoomer XaoS to explore the infinite universe having user formula "sin(z^2)-cos(z^2)+c", we start out here (important for universe identification and formula matching with other zoomer software applications, libraries or frameworks.):
I found the following fractal, which I really like:
As you, perhaps, can see, I have run up against the resolution wall (floor?), which means that the deeper I zoom at that point, the more pixelated and boxy the image gets:
Here is the XaoS source code (*.xpf file contents):
;Position file automatically generated by XaoS 4.2.1
; - a realtime interactive fractal zoomer
;Use xaos -loadpos <filename> to display it
(initstate)
(defaultpalette 0)
(formula 'user)
(usrform "sin(z^2)-cos(z^2)+c")
(angle 90)
(maxiter 1000)
(view -0.241140329885861603 1.09425325699643758E-015 8.78234117374088186E-014 8.78234139599431772E-014)
And Fractal->View... yields:
So I tried to enter the formula into UltraFractal, but no matter what I try, I can't get it to work.
This works:
init:
z = #start
loop:
z = sin(z^#power) + cos(z^#power) + #pixel
This does not work:
init:
z = #start
loop:
z = sin(z^#power) - cos(z^#power) + #pixel
The only difference in the above two is the minus sign.
This does not work:
init:
z = #start
loop:
z2p = z^#power
ss = sin(z2p)
cc = cos(z2p)
z = ss - cc + #pixel
This works:
init:
z = #start
loop:
z2p = z^#power
ss = sin(z2p)
cc = cos(z2p)
z = ss + cc + #pixel
but if I add a line negating cc, it doesn't work again:
init:
z = #start
loop:
z2p = z^#power
ss = sin(z2p)
cc = cos(z2p)
cc = -cc
z = ss + cc + #pixel
Finally, one last example.
This works:
init:
z = #start
loop:
z2p = z^#power
ss = sin(z2p)
cc = cos(z2p)
z = ss + 1 - ( 1 - cc ) + #pixel
but this doesn't:
init:
z = #start
loop:
z2p = z^#power
ss = sin(z2p)
cc = cos(z2p)
z = ss + 1 - ( 1 + cc ) + #pixel
As I think I've proven, negation and subtraction seem to be working. And I keep changing only the minus sign or the subtraction to make it stop working, so I suspect that there is a bug (or an arbitrary limitation of not being able to use this particular formula? - doesn't make sense.)
In the "Fractal Mode" pane, lower right part of screen, I click the third item down, "Switch Mode", and it displays "Contains errors." This is the only helpful feedback I've been able to find.
This is a paid program, so I do not expect to be having this problem. Anyone? (secondary question - what zoomer will render this well? Thanks!)
Here is my current UltraFractal source code:
comment {
This file contains standard fractal types for Ultra Fractal. Many of the
fractal formulas here were written by other formula authors, as noted in the
comments with each formula. All formulas have been edited and simplified by
Frederik Slijkerman.
These formulas are also available as objects for the common.ulb framework in
Standard.ulb.
}
sin2_minus_cos2 {
;
; Generic Mandelbrot set.
;
init:
z = #start
z9 = 1
loop:
z2p = z^#power
ss = sin(z2p)
cc = cos(z2p)
cc = -cc
z = ss + cc + #pixel
bailout:
|z| <= #bailout
$IFDEF VER60
perturbinit:
#dz = 0
perturbloop:
if #power == (2, 0)
#dz = 2 * #z * #dz + sqr(#dz) + #dpixel
elseif #power == (3, 0)
complex z2 = sqr(#z)
complex dz2 = sqr(#dz)
#dz = 3 * z2 * #dz + 3 * #z * dz2 + #dz * dz2 + #dpixel
else ; power 4
complex z2 = sqr(#z)
complex dz2 = sqr(#dz)
complex zdz4 = 4*#z*#dz
#dz = #dpixel + zdz4*z2 + 6*z2*dz2 + zdz4*dz2 + sqr(dz2)
endif
$ENDIF
default:
title = "sin2_minus_cos2"
center = (-0.5, 0)
$IFDEF VER50
rating = recommended
$ENDIF
$IFDEF VER60
perturb = #power == (2, 0) || #power == (3, 0) || #power == (4, 0)
$ENDIF
param start
caption = "Starting point"
default = (0,0)
hint = "The starting point parameter can be used to distort the Mandelbrot \
set. Use (0, 0) for the standard Mandelbrot set."
endparam
param power
caption = "Power"
default = (2,0)
hint = "This parameter sets the exponent for the Mandelbrot formula. \
Increasing the real part to 3, 4, and so on, will add discs to \
the Mandelbrot figure. Non-integer real values and non-zero \
imaginary values will create distorted Mandelbrot sets. Use (2, 0) \
for the standard Mandelbrot set."
endparam
float param bailout
caption = "Bailout value"
default = 4.0
min = 1.0
$IFDEF VER40
exponential = true
$ENDIF
hint = "This parameter defines how soon an orbit bails out while \
iterating. Larger values give smoother outlines; values around 4 \
give more interesting shapes around the set. Values less than 4 \
will distort the fractal."
endparam
switch:
type = "Julia"
seed = #pixel
power = power
bailout = bailout
}
I just punched the formula into a zoomer on my Android, and it seems to work.
Here is that coordinate, zoomed out just a little, so that you can barely see the pixelated fractal (just another example of great beauty from this wonderful infinite universe!):
EDIT:
This formula works:
init:
z = #start
loop:
; in XaoS, user formula sin(z)^2-cos(z)^2+c
z = sin(z)^2 - cos(z)^2 + #pixel
And here's the result in UltraFractal:

How do you find outer points in a circle from a point mathematically?

Trying to find a set of points that would allow a point to seemingly connect to a circle, like the image below however there is no overlap and it lines up with the edge of the circle. Apologies if this is hard to understand it is hard to explain.
If you want something like this:
then the python code below does this.
import numpy as np
# function that calcuates the points at which the lines are tangent to the circle
def tangent_points(Point, Circle):
Rot_90 = np.array([[0, -1],
[1, 0]])
O = Circle[0]
r = Circle[1]
unit_OP = Point - O
OP = np.sqrt( unit_OP.dot(unit_OP) )
unit_OP = unit_OP / OP
a = r**2 / OP
unit_perp = Rot_90.dot(unit_OP)
b = np.sqrt(r**2 - a**2)
return O + a*unit_OP + b*unit_perp, O + a*unit_OP - b*unit_perp
# Test example
O = np.array([0,0])
r = 2
P = np.array([7,5])
Circ = (O, r)
T1, T2 = tangent_points(P, Circ)
# plotting preparations:
# prepare circle
s = np.linspace( 0 , 2 * np.pi , 150 )
xc = O[0] + r*np.cos( s )
yc = O[1] + r*np.sin( s )
# prepare tangents
s = np.linspace( 0, 1, 150)
L1 = P[:, np.newaxis]*(1-s[np.newaxis, :]) + T1[:, np.newaxis]*s[np.newaxis, :]
L2 = P[:, np.newaxis]*(1-s[np.newaxis, :]) + T2[:, np.newaxis]*s[np.newaxis, :]
# actual plotting
# setup a figure environment
figure, axes = plt.subplots()
axes.set_aspect( 1 )
# plotting circle and center
axes.plot( xc, yc )
axes.plot(O[0], O[1], 'bo')
# plotting point ouside circle
axes.plot(P[0], P[1], 'bo')
# plotting tagnetn lines
axes.plot( L1[0], L1[1] )
axes.plot( L2[0], L2[1] )
# plotting tangent points
axes.plot(T1[0], T1[1], 'ro')
axes.plot(T2[0], T2[1], 'ro')
plt.show()

Deleting an image from a tkinter canvas based on its coordinates

I am working on a game where the player has to collect coins by landing on chests. However, if they land on the same chest three times, it will delete the chest that is there and will insert a bandit in its place - which will reset their coins back to zero (this part is not included in the code provided as it runs correctly).
try:
import tkinter as tk
from tkinter import ttk
except ImportError:
import Tkinter as tk
from Tkinter import ttk
import random
from collections import Counter
CELL_WIDTH = 50
CELL_HEIGHT = 50
rows = 8
columns = 8
bandit_number = 5
chest_number = 10
moves = 0
coins = 0
chests_visited = ()
def create_grid():
game_frame.pack()
game_grid.pack()
data = {}
for col in range(columns):
for row in range(rows):
x1 = col * CELL_WIDTH
y1 = row * CELL_HEIGHT
x2 = x1 + CELL_WIDTH
y2 = y1 + CELL_HEIGHT
data[row, col] = game_grid.create_rectangle(x1, y1, x2, y2,
fill="green",
tags="rect")
return data
def create_chests(chest_image):
global chest_dict
chest_dict = {}
for i in range(chest_number):
while True:
row = random.randint(0, rows-1)
col = random.randint(0, columns-1)
if (row,col) not in chest_dict:
break
x1 = col * CELL_WIDTH + 24
y1 = row * CELL_HEIGHT - 26
x2 = x1 + CELL_WIDTH
y2 = y1 + CELL_HEIGHT
chest_dict[row,col] = game_grid.create_image(x1, y1, image=chest_image,
tags="chest")
return chest_dict
def create_bandits(bandit_image):
global bandit_dict
bandit_dict = {}
for i in range(bandit_number):
while True:
row = random.randint(0, rows-1)
col = random.randint(0, columns-1)
if (row,col) not in bandit_dict:
break
x = col * CELL_WIDTH + 22
y = row * CELL_HEIGHT - 22
x2 = x + CELL_WIDTH
y2 = y + CELL_HEIGHT
bandit_dict[row,col] = game_grid.create_image(x, y, image=bandit_image)
return bandit_dict
def position_player(player_image):
global arrow
arrow = game_grid.create_image(26, 375, image=player_image)
display_widgets()
return arrow
def display_widgets():
global move_entry_x, move_entry_y, help_lbl
help_lbl = tk.Label(game_grid, text="Enter the x value in the first entry"+
" and the y value in the second." + '\n' +
"Use negatives to move left and down.")
game_grid.create_window(200, 420, window=help_lbl)
move_entry_x = tk.Entry(game_grid)
game_grid.create_window(70, 450, window=move_entry_x)
move_entry_y = tk.Entry(game_grid)
game_grid.create_window(200, 450, window=move_entry_y)
enter_btn = ttk.Button(game_grid, text="Enter", command=check_move)
game_grid.create_window(305, 450, window=enter_btn)
def check_move():
global help_lbl
if (
move_entry_x.get()[0] == "-" or
move_entry_y.get()[0] == "-"
):
try:
if (
int(move_entry_x.get()[1])*CELL_WIDTH < 26 or
int(move_entry_x.get()[1])*CELL_WIDTH > int(rows)*CELL_WIDTH
):
print("Illegal move! Enter a different value")
elif (
int(move_entry_y.get()[1])*CELL_WIDTH < 26 or
int(move_entry_y.get()[1])*CELL_WIDTH > int(rows)*CELL_HEIGHT
):
print("Illegal move! Enter a different value")
else:
move_player(arrow)
except ValueError:
print("Please enter a number!")
else:
try:
if (
int(move_entry_x.get())*CELL_WIDTH < 26 or
int(move_entry_x.get())*CELL_WIDTH > int(rows)*CELL_WIDTH
):
print("Illegal move! Enter a different value")
elif (
int(move_entry_y.get())*CELL_WIDTH < 26 or
int(move_entry_y.get())*CELL_WIDTH > int(rows)*CELL_HEIGHT
):
print("Illegal move! Enter a different value")
else:
move_player(arrow)
except ValueError:
print("Please enter a number!")
def move_player(arrow):
global move_entry_x, move_entry_y, help_lbl, moves
x_move = move_entry_x.get()
y_move = move_entry_y.get()
x = int(x_move)*CELL_WIDTH
y = int(y_move)*CELL_HEIGHT
game_grid.move(arrow, x, -y)
moves += 1
print("Moves = "+str(moves))
check_position(arrow, chest_dict)
def check_position(arrow, chest_dict):
global coins, arrow_coords, chests_visited
arrow_coords = game_grid.coords(arrow)
for i in chest_dict:
chest_coords = game_grid.coords(chest_dict[i])
if (
int(arrow_coords[0])-2 in chest_coords and
int(arrow_coords[1])-1 in chest_coords
):
coins += 10
chests_visited += tuple(arrow_coords)
print("Chests visited: "+str(chests_visited))
check_chests()
return arrow, chest_dict
def check_chests():
global chests_visited, chest_dict, bandit_dict
cnt = Counter(chests_visited)
if (
[k for k, v in cnt.items() if v == 3]
):
game_grid.create_image(arrow_coords[0],arrow_coords[1],
image=bandit_image)
print("bandit_time")
window = tk.Tk()
game_frame = tk.Frame(window)
game_grid = tk.Canvas(game_frame, width=500, height=500, borderwidth=0,
highlightthickness=0)
game_grid.itemconfig("rect", fill="green")
bandit_image = tk.PhotoImage(file="Bandit.png")
chest_image = tk.PhotoImage(file="Treasure Chest.png")
player_image = tk.PhotoImage(file="Arrow.png")
rects = create_grid()
bandits = create_bandits(bandit_image)
chests = create_chests(chest_image)
player = position_player(player_image)
window.mainloop()
I know you can use canvas.delete(item_id) to remove an object, given that it has been defined but my problem is that since I created my objects with a dictionary, they do not have specific names which I could use and I would like to know how I can delete an object from a canvas based on what it's coordinates are rather than it's name.
Also, as a side note, since I am using images, according to answers I have found on , the format has to be GIF, but I am able to use the PNG format and it still works fine but when I try my game on a different device, I get the expected error. Is there a reason for this?
With the help of furas, I have found a solution:
Since each chest has its own unique row and column, you can refer to an individual chest using its row and column, meaning we must find out what that row and column could be.
Because x1 = col * CELL_WIDTH + 24 and y1 = row * CELL_HEIGHT - 26 where x1 is the x coordinate and y1 is the y coordinate, the equations for the row and column must be (x1 - 24) / CELL_WIDTH = col and (y1 + 26) / CELL_HEIGHT = row.
You would then substitute for the x and y coordinates using arrow_coords[0] and arrow_coords[1]. And to get the specific row and column from the chest_dict, you would then round it to the nearest whole number and it will delete that specific chest from the canvas:
game_grid.delete(chest_dict[int(round(int(arrow_coords[0]-24)/50, 0)),
int(round(int(arrow_coords[1]+26)/50, 0))])

Adding contents in two dimensional list

In Python, using IDLE 3.4 (x64), I have created a two-dimensional list of 3 rows and 2 columns. I then had the list populate with random numbers.
Now I want to display the sum values in the rows and then display the sum of the columns, and I'm at a loss for how to do that.
Here is what I have so far:
import random
def main():
#Create nested loop
rows = 3
cols = 2
values = [[0,0],
[0,0],
[0,0]]
#Generate random integers in list
for r in range(rows):
for c in range(cols):
values[r][c] = random.randint(1, 100)
#Display results
print(values)
main()
Here is what I got:
import random
def main():
#Create nested loop
rows = 3
cols = 2
values = [[0,0],
[0,0],
[0,0]]
#Generate random integers in list
for r in range(rows):
for c in range(cols):
values[r][c] = random.randint(1, 100)
#Display results
print(values)
sum_2 = 0
sum_3 = 0
sum_2_list = []
sum_3_list = []
length = len(values)
for num in range(0, length):
sum = values[num][0] + values[num][1]
print('{} + {} = {}'.format(values[num][0], values[num][1], sum))
for num2 in range(0, length):
sum_2 = sum_2 + values[num2][0]
sum_3 = sum_3 + values[num2][1]
print('{} + {} + {} = {}'.format(values[0][0], values[1][0], values[2][0], sum_2))
print('{} + {} + {} = {}'.format(values[0][1], values[1][1], values[2][1], sum_3))

Resources